Guest User

Untitled

a guest
Jun 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. public class DatabaseHelper extends SQLiteOpenHelper {
  2.  
  3. private Context dbContext;
  4.  
  5. private static String DB_NAME = "appData.db";
  6. private String DB_PATH = "";
  7.  
  8. private static final int DB_VERSION = 1;
  9.  
  10. private SQLiteDatabase mDatabase;
  11.  
  12.  
  13. public DatabaseHelper(Context context) {
  14. super(context, DB_NAME, null, DB_VERSION);
  15. this.dbContext = context;
  16. DB_PATH = dbContext.getApplicationInfo().dataDir + "/databases/";
  17.  
  18. if (checkDataBase()) {
  19. Log.d("database_test", "Open database call");
  20. openDataBase();
  21. } else {
  22. try {
  23. Log.d("database_test", "Open database not call");
  24. this.getReadableDatabase();
  25. Log.d("database_test", "copy database call");
  26. copyDataBase();
  27. this.close();
  28. openDataBase();
  29.  
  30. } catch (IOException e) {
  31. throw new Error("Error copying database");
  32. }
  33. Toast.makeText(context, "Initial database is created", Toast.LENGTH_LONG).show();
  34. }
  35.  
  36. }
  37.  
  38.  
  39. private void copyDataBase() throws IOException {
  40. Log.d("database_test", "Copy database call 2");
  41. InputStream myInput = dbContext.getAssets().open(DB_NAME);
  42. String outFileName = DB_PATH + DB_NAME;
  43. OutputStream myOutput = new FileOutputStream(outFileName);
  44.  
  45. byte[] buffer = new byte[1024];
  46. int length;
  47. while ((length = myInput.read(buffer)) > 0) {
  48. myOutput.write(buffer, 0, length);
  49. }
  50.  
  51. myOutput.flush();
  52. myOutput.close();
  53. myInput.close();
  54. Log.d("database_test", "Open database worked finish");
  55. }
  56.  
  57. public void openDataBase() throws SQLException {
  58. Log.d("database_test", "Open database call 2");
  59. String dbPath = DB_PATH + DB_NAME;
  60. mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
  61. }
  62.  
  63.  
  64. private boolean checkDataBase() {
  65. SQLiteDatabase checkDB = null;
  66. boolean exist = false;
  67. try {
  68. String dbPath = DB_PATH + DB_NAME;
  69. checkDB = SQLiteDatabase.openDatabase(dbPath, null,
  70. SQLiteDatabase.OPEN_READONLY);
  71. } catch (SQLiteException e) {
  72. Log.v("db_log", "database does't exist");
  73. }
  74.  
  75. if (checkDB != null) {
  76. exist = true;
  77. checkDB.close();
  78. }
  79. return exist;
  80. }
  81.  
  82. public void getAllTableName() {
  83. mDatabase = getReadableDatabase();
  84. Cursor c = mDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
  85.  
  86. if (c.moveToFirst()) {
  87. while (!c.isAfterLast()) {
  88. Log.d("db_test", "table list: " + c.getString(0));
  89. // Toast.makeText(acti, "Table Name=> "+c.getString(0), Toast.LENGTH_LONG).show();
  90. c.moveToNext();
  91. }
  92. }
  93. c.close();
  94. }
  95.  
  96. public Cursor getTestData() {
  97. Cursor cursor = mDatabase.rawQuery("SELECT * FROM qna", null);
  98. /*if (cursor != null) {
  99. while (cursor.moveToNext()) {
  100.  
  101. String value = cursor.getString(1);
  102. Log.d("db_test", "table value: " + value);
  103. }
  104. // cursor.close();
  105. }*/
  106.  
  107. return cursor;
  108. }
  109.  
  110.  
  111.  
  112.  
  113. @Override
  114. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  115.  
  116. }
  117.  
  118. @Override
  119. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  120.  
  121. }
  122.  
  123.  
  124. }
Add Comment
Please, Sign In to add comment