Guest User

Untitled

a guest
Jan 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. SystemDatabaseHelper dbHelper = new SystemDatabaseHelper(this, null, 1);
  2. dbHelper.initialize(this);
  3.  
  4. public class ContactDBHelper extends SQLiteOpenHelper {
  5.  
  6. private ContactDBHelper(Context context, String name,
  7. CursorFactory factory, int version) {
  8. super(context, name, factory, version);
  9.  
  10. }
  11.  
  12. private static ContactDBHelper mInstance;
  13.  
  14. public static synchronized ContactDBHelper getInstance() {
  15.  
  16. if (mInstance == null) {
  17. mInstance = new ContactDBHelper(Util.getApplicationContext(),
  18. ContactDB.DB_NAME, null, ContactDB.DB_VERSION);
  19. }
  20. return mInstance;
  21. }
  22.  
  23. @Override
  24. public void onCreate(SQLiteDatabase db) {
  25. db.execSQL(ContactDB.Contact.CREATE_STMT);
  26. db.execSQL(ContactDB.Contact.CREATE_PHONE_NUMBER_INDEX);
  27. db.execSQL(ContactDB.Contact.CREATE_REVERSE_PHONE_NUMBER_INDEX);
  28. }
  29.  
  30. @Override
  31. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  32. // TODO Auto-generated method stub
  33.  
  34. }
  35.  
  36. @Override
  37. protected Object clone() throws CloneNotSupportedException {
  38.  
  39. throw new CloneNotSupportedException();
  40. }
  41.  
  42. }
  43.  
  44. public class Util {
  45.  
  46. private static WeakReference<Context> applicationContext;
  47.  
  48. public static Context getApplicationContext() {
  49. return applicationContext.get();
  50. }
  51.  
  52. public static void setApplicationContext(Context context) {
  53. applicationContext = new WeakReference<Context>(context);
  54. }
  55. }
  56.  
  57. public class NameOfApp extends Application {
  58.  
  59. public static SQLiteDatabase db;
  60.  
  61. @Override
  62. public void onCreate() {
  63.  
  64. super.onCreate();
  65.  
  66. try{
  67.  
  68. // this will create the database if required (e.g. new install or db deleted)
  69. db=new Database(this.getBaseContext()).getWritableDatabase();
  70.  
  71. } catch (Exception e) {
  72.  
  73. // TODO add alert and quit
  74. Log.e(TAG,"Error creating DB:" + e.getMessage());
  75. Toast.makeText(getApplicationContext(), "Error creating DB:" + e.getMessage(), Toast.LENGTH_LONG).show();
  76.  
  77. }
  78.  
  79. } // onCreate
  80.  
  81. @Override
  82. public void onTerminate(){
  83. Log.d(TAG, "Application.onTerminate() database closed");
  84. super.onTerminate();
  85. savePreferences();
  86. db.close();
  87. }
  88.  
  89. public void closeDB(){
  90. db.close();
  91. Log.d(TAG,"Database closed on request");
  92. }
  93.  
  94. protected SQLiteDatabase getwritableDatabase(){
  95. return db;
  96. }
  97.  
  98. }
Add Comment
Please, Sign In to add comment