am_dot_com

DDM 2021-12-07

Dec 7th, 2021 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. package com.joythis.android.simplestsqlite;
  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.util.Log;
  9.  
  10. import androidx.annotation.Nullable;
  11.  
  12. public class MyContactDB extends SQLiteOpenHelper {
  13.  
  14. public final static String DB_NAME = "my_contacts.DB";
  15. //increases in DB_VERSION should cause "onUpgrade"
  16. public final static int DB_VERSION = 1;
  17.  
  18. public final static String TABLE_CONTACTS = "tContacts";
  19. public final static String COL_NAME = "cName";
  20. public final static String COL_PHONE = "cPhone";
  21.  
  22. /*
  23. create table if not exists tContacts(
  24. _id INTEGER PRIMARY KEY NOT NULL,
  25. cName TEXT NOT NULL,
  26. cPhone TEXT NOT NULL
  27. );
  28. */
  29. public final static String CREATE_TABLE_CONTACTS =
  30. "create table if not exists "+TABLE_CONTACTS+"(\n"+
  31. "_id INTEGER PRIMARY KEY NOT NULL,\n"+
  32. COL_NAME+" TEXT NOT NULL,\n"+
  33. COL_PHONE+" TEXT NOT NULL\n);";
  34.  
  35. public final static String DROP_TABLE_CONTACTS =
  36. "drop table if exists "+TABLE_CONTACTS+";";
  37.  
  38. public MyContactDB(
  39. @Nullable Context context, //context
  40. @Nullable String name, //DB_NAME
  41. @Nullable SQLiteDatabase.CursorFactory factory, //
  42. int version //DB_VERSION
  43. ) {
  44. super(context, name, factory, version);
  45. }//MyContactDB
  46.  
  47. public MyContactDB(
  48. @Nullable Context context //context
  49. ){
  50. super(
  51. context,
  52. DB_NAME,
  53. null,
  54. DB_VERSION
  55. );
  56. }//MyContact
  57.  
  58.  
  59. @Override
  60. public void onCreate(SQLiteDatabase db) {
  61. //automatically called by the framework
  62. //when the need to work with still non-existing
  63. //tables
  64. installDB(db);
  65. }//onCreate
  66.  
  67. void installDB(SQLiteDatabase pDB){
  68. if (pDB!=null){
  69. try {
  70. pDB.execSQL(CREATE_TABLE_CONTACTS);
  71. }
  72. catch (Exception e){
  73. Log.e(
  74. "@MyContactDB",
  75. e.toString()
  76. );
  77. }
  78. }
  79. }//installDB
  80.  
  81. @Override
  82. public void onUpgrade(
  83. SQLiteDatabase db,
  84. int oldVersion, //1
  85. int newVersion //2
  86. )
  87. {
  88. //automatically called when
  89. //the database changes
  90. //that is signaled by the database "version"
  91. if (newVersion>oldVersion){
  92. try{
  93. //destroy
  94. db.execSQL(
  95. DROP_TABLE_CONTACTS
  96. );
  97. //reconstruct
  98. installDB(db);
  99. }//try
  100. catch (Exception e){
  101. Log.e(
  102. "@MyContactsDB",
  103. e.toString()
  104. );
  105. }//catch
  106. }//if
  107. }//onUpgrade
  108.  
  109. public final int DOES_NOT_EXIST = -1;
  110. /*
  111. receives a MyContact
  112. returns the _id in TABLE_CONTACTS
  113. where that MyContact already exists
  114. or
  115. -1, if it does not exist
  116. */
  117. public long getIdForContact(MyContact pC){
  118. SQLiteDatabase db = this.getReadableDatabase();
  119. if (db!=null){
  120. //select * from tContacts where (cName='Rita' and cPhone='123')
  121. String strQ = String.format(
  122. "select %s from %s where (%s='%s' and %s='%s')",
  123. //COL_NAME+","+COL_PHONE
  124. "*",
  125. TABLE_CONTACTS,
  126. COL_NAME,
  127. pC.mName,
  128. COL_PHONE,
  129. pC.mPhone
  130. );
  131. Cursor cursorResults =
  132. db.rawQuery(
  133. strQ,
  134. null
  135. );
  136.  
  137. if (cursorResults!=null){
  138. int iHowMany = cursorResults.getCount();
  139. if (iHowMany>0){
  140. //a contact with that name and phone
  141. //already exists
  142. cursorResults.moveToFirst();
  143. int indexOfIdColumn =
  144. cursorResults.getColumnIndex(
  145. "_id"
  146. );
  147. long idOfAlreadyExistingContact =
  148. cursorResults.getLong(
  149. //0
  150. indexOfIdColumn
  151. );
  152. return idOfAlreadyExistingContact;
  153. }//if
  154. }
  155. }
  156. return DOES_NOT_EXIST;
  157. }//getIdForContact
  158.  
  159. long insertNewContact(MyContact pC){
  160. long iCurrentIdForContact =
  161. getIdForContact(pC);
  162.  
  163. boolean bWillBeNew =
  164. iCurrentIdForContact == DOES_NOT_EXIST;
  165.  
  166. if (bWillBeNew){
  167. SQLiteDatabase db =
  168. this.getWritableDatabase();
  169. if (db!=null){
  170. ContentValues cvs = new ContentValues();
  171. cvs.put(COL_NAME, pC.mName);
  172. cvs.put(COL_PHONE, pC.mPhone);
  173.  
  174. long lWhereInserted =
  175. db.insert(
  176. TABLE_CONTACTS,
  177. null,//we have no columns allowing null
  178. cvs
  179. );
  180.  
  181. return lWhereInserted;
  182. }
  183. }
  184.  
  185. return iCurrentIdForContact;
  186. }//insertNewContact
  187. }//MyContactDB
  188.  
Add Comment
Please, Sign In to add comment