Guest User

Untitled

a guest
Oct 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. package com.example.asus.madproject.database;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. import com.example.asus.madproject.R;
  13.  
  14. public class DatabaseHelper extends SQLiteOpenHelper{
  15. public static final String DATABASE_NAME = "mad_projectc.db";
  16. public static final String TABLE_NAME = "user_details";
  17. public static final String COL_1 = "ID";
  18. public static final String COL_2 = "FIRST_NAME";
  19. public static final String COL_3 = "LAST_NAME";
  20. public static final String COL_4 = "EMAIL";
  21. public static final String COL_5 = "PASSWORD";
  22. public static final String COL_6 = "PHONE";
  23.  
  24.  
  25.  
  26. //STUDENT TABLE
  27.  
  28. public static final String STABLE_NAME = "Student_Table";
  29. public static final String SCOL_1 = "SID";
  30. public static final String SCOL_2 = "SFIRST_NAME";
  31. public static final String SCOL_3 = "SLAST_NAME";
  32. public static final String SCOL_4 = "SEMAIL";
  33. public static final String SCOL_5 = "SPASSWORD";
  34. public static final String SCOL_6 = "SPHONE";
  35.  
  36.  
  37.  
  38. public DatabaseHelper(Context context) {
  39. super(context, DATABASE_NAME, null, 1);
  40. }
  41.  
  42.  
  43. @Override
  44. public void onCreate(SQLiteDatabase db) {
  45. db.execSQL("CREATE TABLE " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRST_NAME TEXT, LAST_NAME TEXT, EMAIL TEXT, PASSWORD TEXT, PHONE TEXT)");
  46. //db.execSQL("CREATE TABLE " + STABLE_NAME +" (SID INTEGER PRIMARY KEY AUTOINCREMENT, SFIRST_NAME TEXT, SLAST_NAME TEXT, SEMAIL TEXT, SPASSWORD TEXT, SPHONE TEXT)");
  47.  
  48. }
  49.  
  50. @Override
  51. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  52. db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
  53. //db.execSQL("DROP TABLE IF EXISTS "+STABLE_NAME);
  54. onCreate(db);
  55. }
  56. //inserting data in database
  57. public boolean insertData(String fname, String lname, String email, String password, String phone){
  58. SQLiteDatabase db = this.getWritableDatabase();
  59. ContentValues contentValues = new ContentValues();
  60. contentValues.put(COL_2,fname);
  61. contentValues.put(COL_3,lname);
  62. contentValues.put(COL_4,email);
  63. contentValues.put(COL_5,password);
  64. contentValues.put(COL_6,phone);
  65. long result = db.insert(TABLE_NAME,null,contentValues);
  66. if (result == -1)
  67. return false;
  68. else
  69. return true;
  70. }
  71. //checking the email and password
  72. public boolean ckeckLogin(String email, String password){
  73. SQLiteDatabase db = this.getReadableDatabase();
  74. Cursor cursor = db.rawQuery("SELECT * FROM " +TABLE_NAME+ " WHERE " +COL_4+ "=? AND " +COL_5+ "=? ", new String[]{email,password});
  75.  
  76. if (cursor.getCount() > 0) {
  77. //cursor.moveToNext();
  78. return true;
  79. } else
  80. return false;
  81. }
  82. //checking if email exists
  83. public boolean checkEmail(String email){
  84. SQLiteDatabase db = this.getReadableDatabase();
  85. Cursor cursor = db.rawQuery("SELECT * FROM " +TABLE_NAME+ " WHERE " +COL_4+ "=? ",new String[]{email});
  86. if (cursor.getCount() > 0)
  87. return false;
  88. else
  89. return true;
  90. }
  91. //get data from database
  92. public Cursor viewData(){
  93. SQLiteDatabase db = this.getReadableDatabase();
  94. Cursor res = db.rawQuery("SELECT * FROM " +TABLE_NAME,null);
  95. return res;
  96. }
  97. //update data
  98. public boolean updateData(String id,String fname, String lname, String email, String password, String phone){
  99. SQLiteDatabase db = this.getWritableDatabase();
  100. ContentValues contentValues = new ContentValues();
  101.  
  102. contentValues.put(COL_1,id);
  103. contentValues.put(COL_2,fname);
  104. contentValues.put(COL_3,lname);
  105. contentValues.put(COL_4,email);
  106. contentValues.put(COL_5,password);
  107. contentValues.put(COL_6,phone);
  108. db.update(TABLE_NAME, contentValues, " ID = ? ",new String[]{id});
  109. return true;
  110. }
  111. //delete data
  112. public Integer deleteData(String email){
  113. SQLiteDatabase db = this.getWritableDatabase();
  114. return db.delete(TABLE_NAME, " ID = ? ", new String[]{email});
  115. }
  116.  
  117. //inserting data in database
  118. public boolean insertStudentData(String fname, String lname, String email, String password, String phone){
  119. SQLiteDatabase db = this.getWritableDatabase();
  120. ContentValues contentValues = new ContentValues();
  121. contentValues.put(SCOL_2,fname);
  122. contentValues.put(SCOL_3,lname);
  123. contentValues.put(SCOL_4,email);
  124. contentValues.put(SCOL_5,password);
  125. contentValues.put(SCOL_6,phone);
  126. long result = db.insert(TABLE_NAME,null,contentValues);
  127. if (result == -1)
  128. return false;
  129. else
  130. return true;
  131. }
  132.  
  133. public Cursor search(String id){
  134.  
  135. SQLiteDatabase db = this.getReadableDatabase();
  136.  
  137. Cursor res = db.rawQuery("SELECT * FROM " +TABLE_NAME+" where ID='"+id+"'",null);
  138. return res;
  139. }
  140.  
  141. public Cursor searchname(String name){
  142.  
  143. SQLiteDatabase db = this.getReadableDatabase();
  144.  
  145. Cursor res = db.rawQuery("SELECT * FROM " +TABLE_NAME+" where FIRST_NAME='"+name+"'",null);
  146. return res;
  147. }
Add Comment
Please, Sign In to add comment