Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. package net.accedegh.studentregistration.models;
  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.  
  9. public class StudentDbHelper extends SQLiteOpenHelper {
  10.  
  11. private static final String DATABASE = "StudentRegDb";
  12. private static final String TABLE ="Student";
  13. private static final String COLUMN_ID = "_id";
  14. private static final String COLUMN_NAME = "Name";
  15. private static final String COLUMN_PHONE ="Phone";
  16. private static final String COLUMN_EMAIL ="Email";
  17. private static final String COLUMN_TAG ="Tag";
  18.  
  19.  
  20. public StudentDbHelper(Context context) {
  21. super(context, DATABASE, null, 1);
  22. SQLiteDatabase db = getWritableDatabase();
  23. }
  24.  
  25. @Override
  26. public void onCreate(SQLiteDatabase db) {
  27.  
  28. db.execSQL("create table Student " +
  29. "(_id INTEGER PRIMARY KEY AUTOINCREMENT,Name Text,Phone Text, Email Text, Tag Text)");
  30.  
  31. }
  32.  
  33. @Override
  34. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  35. db.execSQL("drop table if exists "+TABLE);
  36. onCreate(db);
  37. }
  38.  
  39.  
  40. public boolean addStudent(String name, String phone, String email, String tag) {
  41. SQLiteDatabase db = getWritableDatabase();
  42. ContentValues cv = new ContentValues();
  43. cv.put(COLUMN_NAME, name);
  44. cv.put(COLUMN_PHONE, phone);
  45. cv.put(COLUMN_EMAIL, email);
  46. cv.put(COLUMN_TAG, tag);
  47. long result = db.insert(TABLE, null, cv);
  48. if(result == -1){
  49. return false;
  50. }
  51. else{
  52. return true;
  53. }
  54. }
  55.  
  56. public Cursor getAllStudents(){
  57. SQLiteDatabase db = getWritableDatabase();
  58. Cursor data = db.rawQuery("select * from " + TABLE,null );
  59. return data;
  60.  
  61. }
  62.  
  63.  
  64. public Cursor getAllStudetById(String id){
  65. SQLiteDatabase db = getWritableDatabase();
  66. String query ="select * from " + TABLE + " where _id = ? ";
  67. Cursor data = db.rawQuery(query, new String[]{id});
  68. return data;
  69. }
  70.  
  71. public void deleteStudent(String studentId){
  72. SQLiteDatabase db = getWritableDatabase();
  73. db.execSQL("delete from " +TABLE + " where _id = " +studentId + "");
  74. //db.delete()
  75.  
  76. }
  77.  
  78. public Cursor searchStudent(String queryText){
  79. SQLiteDatabase db = getWritableDatabase();
  80. String[] args = new String[1];
  81. args[0] = "%"+queryText+"%";
  82. String query ="select * from Student where Name Like ?";
  83. Cursor cursor = db.rawQuery(query,args);
  84. return cursor;
  85. }
  86.  
  87.  
  88. public void UpdateStudent(String id, String name, String phone, String email, String tag){
  89. SQLiteDatabase db = getWritableDatabase();
  90. ContentValues cv = new ContentValues();
  91. String where = "_id=?";
  92. cv.put("Name", name);
  93. cv.put("Email", email);
  94. cv.put("Phone",phone);
  95. cv.put("Tag",tag);
  96. db.update(TABLE,cv,"_id=?",new String[]{id});
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement