Advertisement
stsharin

Untitled

Dec 6th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. package com.example.diustudentsupport;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.SQLException;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10.  
  11. public class DBHelper {
  12.  
  13. // teacher info table columns
  14. public static final String COL_ID = "id";
  15. public static final String COL_NAME = "name";
  16. public static final String COL_TI = "teacher_initial";
  17. public static final String COL_EMP_ID = "employee_id";
  18. public static final String COL_DESIGNATION = "designation";
  19. public static final String COL_DEPARTMENT = "department";
  20. public static final String COL_FACULTY = "faculty";
  21. public static final String COL_P_WEB_PAGE = "personal_web_page";
  22. public static final String COL_EMAIL = "email";
  23. public static final String COL_CELL_PH_NO = "cell_phone_no";
  24. public static final String KEY_SEARCH = "searchData";
  25.  
  26. private static final String TAG = "DBHelper";
  27. private DatabaseHelper mDbHelper;
  28. private SQLiteDatabase mDb;
  29.  
  30. private static final String DATABASE_NAME = "StudentSupport";
  31. private static final String FTS_VIRTUAL_TABLE = "TeacherInfo"; //TABLE NAME
  32. private static final int DATABASE_VERSION = 1;
  33.  
  34. //Create a FTS3 Virtual Table for fast searches
  35. private static final String DATABASE_CREATE =
  36. "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + " USING fts3(" +
  37. COL_NAME + "," +
  38. COL_TI + "," +
  39. COL_EMP_ID + "," +
  40. COL_DESIGNATION + "," +
  41. COL_DEPARTMENT + "," +
  42. COL_FACULTY + "," +
  43. COL_P_WEB_PAGE + "," +
  44. COL_EMAIL + "," +
  45. COL_CELL_PH_NO + "," +
  46. KEY_SEARCH + "," +
  47. " UNIQUE (" + COL_NAME + "));";
  48.  
  49.  
  50. private final Context mCtx;
  51.  
  52. private static class DatabaseHelper extends SQLiteOpenHelper {
  53.  
  54. DatabaseHelper(Context context) {
  55. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  56. }
  57.  
  58.  
  59. @Override
  60. public void onCreate(SQLiteDatabase db) {
  61. Log.w(TAG, DATABASE_CREATE);
  62. db.execSQL(DATABASE_CREATE);
  63. }
  64.  
  65. @Override
  66. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  67. Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  68. + newVersion + ", which will destroy all old data");
  69. db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
  70. onCreate(db);
  71. }
  72. }
  73.  
  74. public DBHelper(Context ctx) {
  75. this.mCtx = ctx;
  76. }
  77.  
  78. public DBHelper open() throws SQLException {
  79. mDbHelper = new DatabaseHelper(mCtx);
  80. mDb = mDbHelper.getWritableDatabase();
  81. return this;
  82. }
  83.  
  84. public void close() {
  85. if (mDbHelper != null) {
  86. mDbHelper.close();
  87. }
  88. }
  89.  
  90. public long addInfo(String name, String teacher_initial, String em_id, String designation, String dept,
  91. String faculty, String p_webpage, String email, String cell) {
  92.  
  93. ContentValues initialValues = new ContentValues();
  94. String searchValue =
  95. name + " " +
  96. teacher_initial + " " +
  97. em_id + " " +
  98. designation + " " +
  99. dept + " " +
  100. faculty + " " +
  101. p_webpage + " " +
  102. email + " " +
  103. cell;
  104. initialValues.put(COL_NAME, name);
  105. initialValues.put(COL_TI, teacher_initial);
  106. initialValues.put(COL_EMP_ID, em_id);
  107. initialValues.put(COL_DESIGNATION, designation);
  108. initialValues.put(COL_DEPARTMENT, dept);
  109. initialValues.put(COL_FACULTY, faculty);
  110. initialValues.put(COL_P_WEB_PAGE, p_webpage);
  111. initialValues.put(COL_EMAIL, email);
  112. initialValues.put(COL_CELL_PH_NO, cell);
  113. initialValues.put(KEY_SEARCH, searchValue);
  114.  
  115. return mDb.insert(FTS_VIRTUAL_TABLE, null, initialValues);
  116. }
  117.  
  118. // Search function
  119. public Cursor searchInfo(String inputText) throws SQLException {
  120. Log.w(TAG, inputText);
  121. String query = "SELECT docid as _id," +
  122. COL_NAME + "," +
  123. COL_TI + "," +
  124. COL_EMP_ID + "," +
  125. COL_DESIGNATION + "," +
  126. COL_DEPARTMENT + "," +
  127. COL_FACULTY + "," +
  128. COL_P_WEB_PAGE + "," +
  129. COL_EMAIL + "," +
  130. COL_CELL_PH_NO +
  131. " from " + FTS_VIRTUAL_TABLE +
  132. " where " + KEY_SEARCH + " MATCH '" + inputText + "';";
  133. Log.w(TAG, query);
  134. Cursor mCursor = mDb.rawQuery(query,null);
  135.  
  136. if (mCursor != null) {
  137. mCursor.moveToFirst();
  138. }
  139. return mCursor;
  140.  
  141. }
  142.  
  143. public boolean deleteAllInfo() {
  144.  
  145. int doneDelete = 0;
  146. doneDelete = mDb.delete(FTS_VIRTUAL_TABLE, null , null);
  147. Log.w(TAG, Integer.toString(doneDelete));
  148. return doneDelete > 0;
  149.  
  150. }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement