Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. public class DBHelper2 extends SQLiteOpenHelper {
  2. public DBHelper2(Context context) {
  3. super(context, Constants.DB_NAME2, null, Constants.DB_VERSION);
  4. }
  5.  
  6. @Override
  7. public void onCreate(SQLiteDatabase db) {
  8.  
  9. db.execSQL(Constants.CREATE_TB2);
  10.  
  11. }
  12.  
  13. @Override
  14. public void onUpgrade(SQLiteDatabase db, int i, int i1) {
  15. db.execSQL(Constants.DROP_TB2);
  16. onCreate(db);
  17. }
  18. }
  19.  
  20. public class DBAdapter2 {
  21. Context c;
  22. SQLiteDatabase db;
  23. DBHelper2 helper;
  24.  
  25. public DBAdapter2(Context c) {
  26. this.c = c;
  27. helper = new DBHelper2(c);
  28. }
  29.  
  30.  
  31. public void openDB2() {
  32. try {
  33. db = helper.getWritableDatabase();
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38.  
  39. public void closeDB2() {
  40. try {
  41. helper.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46.  
  47.  
  48. //REGISTER
  49. public boolean register(String username, String password) {
  50. try {
  51. ContentValues cv = new ContentValues();
  52. cv.put(Constants.USERNAME, username);
  53. cv.put(Constants.PASSWORD, password);
  54. long result = db.insert(Constants.TB_NAME2, Constants.ROW_ID2, cv);
  55. if (result > 0) {
  56. return true;
  57. }
  58. } catch (SQLException e) {
  59. e.printStackTrace();
  60. }
  61. return false;
  62. }
  63.  
  64. public String getSinlgeEntry(String userName)
  65. {
  66. Cursor cursor=db.query(Constants.DB_NAME2, null, Constants.USERNAME +"=?", new String[]{userName}, null, null, null);
  67. if(cursor.getCount()<1) // UserName Not Exist
  68. {
  69. cursor.close();
  70. return "NOT EXIST";
  71. }
  72. cursor.moveToFirst();
  73. String password= cursor.getString(cursor.getColumnIndex(Constants.PASSWORD));
  74. cursor.close();
  75. return password;
  76. }
  77.  
  78. }
  79.  
  80. public class Login extends AppCompatActivity {
  81.  
  82. Button btnSignIn,btnSignUp;
  83. DBAdapter2 db;
  84. EditText editTextUserName,editTextPassword;
  85.  
  86. @Override
  87. protected void onCreate(Bundle savedInstanceState)
  88. {
  89. super.onCreate(savedInstanceState);
  90. setContentView(R.layout.activity_login);
  91. editTextUserName=(EditText)findViewById(R.id.editTextUserNameToLogin);
  92. editTextPassword=(EditText)findViewById(R.id.editTextPasswordToLogin);
  93. // create a instance of SQLite Database
  94. db=new DBAdapter2(this);
  95. db.openDB2();
  96.  
  97. // Get The Refference Of Buttons
  98. btnSignIn=(Button)findViewById(R.id.buttonSignIn);
  99. btnSignUp=(Button)findViewById(R.id.buttonRegister);
  100.  
  101. // Set OnClick Listener on SignUp button
  102. btnSignUp.setOnClickListener(new View.OnClickListener() {
  103. public void onClick(View v) {
  104. // TODO Auto-generated method stub
  105.  
  106. /// Create Intent for SignUpActivity abd Start The Activity
  107. Intent intentSignUP=new Intent(getApplicationContext(),Register.class);
  108. startActivity(intentSignUP);
  109. }
  110. });
  111. btnSignIn.setOnClickListener(new View.OnClickListener() {
  112.  
  113. public void onClick(View v) {
  114. // get The User name and Password
  115. String userName=editTextUserName.getText().toString();
  116. String password=editTextPassword.getText().toString();
  117.  
  118. // fetch the Password form database for respective user name
  119. String storedPassword=db.getSinlgeEntry(userName);
  120.  
  121. // check if the Stored password matches with Password entered by user
  122. if(password.equals(storedPassword))
  123. {
  124. Toast.makeText(Login.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
  125. Intent intent = new Intent(Login.this,MainActivity.class);
  126. startActivity(intent);
  127. }
  128. else
  129. {
  130. Toast.makeText(Login.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
  131. }
  132. }
  133. });
  134.  
  135. }
  136. @Override
  137. protected void onDestroy() {
  138. super.onDestroy();
  139. // Close The Database
  140. db.closeDB2();
  141. }
  142.  
  143. public class Constants {
  144. //columns
  145.  
  146. static final String ROW_ID="id";
  147. static final String ROW_ID2="id2";
  148.  
  149. static final String NAME="name";
  150. static final String NUMBER="number";
  151. static final String USERNAME ="username";
  152. static final String PASSWORD = "password";
  153.  
  154.  
  155. //properties
  156. static final String DB_NAME="hh_DB";
  157. static final String DB_NAME2="hh2_DB";
  158.  
  159. static final String TB_NAME="hh_TB";
  160. static final String TB_NAME2="hh2_TB";
  161.  
  162. static final int DB_VERSION=1;
  163.  
  164. //creating tb
  165. static final String CREATE_TB="CREATE TABLE hh_TB(id INTEGER PRIMARY KEY AUTOINCREMENT,"
  166. + "name TEXT NOT NULL," + "number TEXT NOT NULL );";
  167. static final String CREATE_TB2="CREATE TABLE hh2_TB(id2 INTEGER PRIMARY KEY AUTOINCREMENT,"
  168. + "username TEXT NOT NULL ," + "password TEXT NOT NULL);";
  169.  
  170.  
  171. //DROP TB
  172. static final String DROP_TB="DROP TABLE IF EXISTS " +TB_NAME;
  173. static final String DROP_TB2="DROP TABLE IF EXISTS " +TB_NAME2;
  174.  
  175. android.database.sqlite.SQLiteException: no such table: hh2_DB (code 1): , while compiling: SELECT * FROM hh2_DB WHERE username=?
  176. at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  177. at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
  178. at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
  179. at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
  180. at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
  181. at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
  182. at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
  183. at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
  184. at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
  185. at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
  186. at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
  187. at com.example.mike.phonebook3.mDataBase.DBAdapter2.getSinlgeEntry(DBAdapter2.java:59)
  188. at com.example.mike.phonebook3.Login$2.onClick(Login.java:53)
  189. at android.view.View.performClick(View.java:4443)
  190. at android.view.View$PerformClick.run(View.java:18443)
  191. at android.os.Handler.handleCallback(Handler.java:733)
  192. at android.os.Handler.dispatchMessage(Handler.java:95)
  193. at android.os.Looper.loop(Looper.java:136)
  194. at android.app.ActivityThread.main(ActivityThread.java:5001)
  195. at java.lang.reflect.Method.invokeNative(Native Method)
  196. at java.lang.reflect.Method.invoke(Method.java:515)
  197. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
  198. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
  199. at dalvik.system.NativeStart.main(Native Method)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement