Guest User

Untitled

a guest
Jan 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. private static final String Database_path = "/data/data/relativlayoutjava/databases/";
  2. private static final String Database_name = "computer.db";//NAME of database stored in Assets folder
  3. private static final String Table_name = "computer";//name of table
  4. private static final String uid = "_id";//name of column1
  5. private static final String Question = "Question";//name of column2
  6. private static final String OptionA = "OptionA";//name of column3
  7. private static final String OptionB = "OptionB";//name of column4
  8. private static final String OptionC = "OptionC";//name of column5
  9. private static final String OptionD = "OptionD";//name of column6
  10. private static final String Answer = "Answer";//name of column7
  11. private static final int version = 1;//version of database signifies if there is any upgradation or not
  12. public SQLiteDatabase sqlite;//object of type SQLiteDatabase
  13. private Context context;//Context object to get context from Question Activity
  14.  
  15. public computer(Context context) {//constructor
  16. super(context, Database_name, null, version);
  17. this.context = context;
  18. }
  19.  
  20. public void createDatabase() {
  21. createDB();
  22. }
  23.  
  24. private void createDB() {
  25.  
  26. boolean dbexist = DBexists();//calling the function to check db exists or not
  27. if (!dbexist)//if database doesnot exist
  28. {
  29.  
  30. this.getReadableDatabase();//Create an empty file
  31. copyDBfromResource();//copy the database file information of assets folder to newly create file
  32. }
  33. }
  34.  
  35. private void copyDBfromResource() {
  36.  
  37. InputStream is;
  38. OutputStream os;
  39. String filePath = Database_path + Database_name;
  40. try {
  41. is = context.getAssets().open(Database_name);//reading purpose
  42. os = new FileOutputStream(filePath);//writing purpose
  43. byte[] buffer = new byte[1024];
  44. int length;
  45. while ((length = is.read(buffer)) > 0) {
  46. os.write(buffer, 0, length);//writing to file
  47. }
  48. os.flush();//flush the outputstream
  49. is.close();//close the inputstream
  50. os.close();//close the outputstream
  51.  
  52. } catch (IOException e) {
  53. throw new Error("Problem copying database file:");
  54. }
  55. }
  56.  
  57. public void openDatabase() throws SQLException//called by onCreate method of Questions Activity
  58. {
  59.  
  60. String myPath = Database_path + Database_name;
  61. sqlite = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
  62. }
  63.  
  64. private boolean DBexists()//Check whether the db file exists or not
  65. {
  66. SQLiteDatabase db = null;
  67. try {
  68. String databasePath = Database_path + Database_name;
  69. db = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
  70. db.setLocale(Locale.getDefault());
  71. db.setVersion(1);
  72. db.setLockingEnabled(true);
  73. } catch (SQLException e) {
  74. Log.e("Sqlite", "Database not found");
  75. }
  76. if (db != null)
  77. db.close();///close the opened file
  78. return db != null ? true : false;
  79.  
  80. }
  81.  
  82. public String readQuestion(int i)//Used to read the data from the Des.db file where id is given and we choose id randomly
  83. {
  84. String Ans = "";//string that contains the required field note that Ans is just a local string not related to Answer or Option...
  85. Cursor c = sqlite.rawQuery("SELECT " + Question + " FROM " + Table_name + " WHERE " + uid + " = " + i + "", null);//cursor to that query
  86. if (c.moveToFirst())
  87. Ans = c.getString(0);
  88. else
  89. Ans = "";
  90. return Ans;
  91. }
  92.  
  93. public String readOptionA(int i)//Used to read the data from the Des.db file where id is given and we choose id randomly
  94. {
  95. String Ans = "";//string that contains the required field note that Ans is just a local string not related to Answer or Option...
  96. Cursor c = sqlite.rawQuery("SELECT " + OptionA + " FROM " + Table_name + " WHERE " + uid + " = " + i + "", null);//cursor to that query
  97. if (c.moveToFirst())
  98. Ans = c.getString(0);
  99. else
  100. Ans = "";
  101. return Ans;
  102. }
  103.  
  104. public String readOptionB(int i)//Used to read the data from the Des.db file where id is given and we choose id randomly
  105. {
  106. String Ans = "";//string that contains the required field note that Ans is just a local string not related to Answer or Option...
  107. Cursor c = sqlite.rawQuery("SELECT " + OptionB + " FROM " + Table_name + " WHERE " + uid + " = " + i + "", null);//cursor to that query
  108. if (c.moveToFirst())
  109. Ans = c.getString(0);
  110. else
  111. Ans = "";
  112. return Ans;
  113. }
  114.  
  115. public String readOptionC(int i)//Used to read the data from the Des.db file where id is given and we choose id randomly
  116. {
  117. String Ans = "";//string that contains the required field note that Ans is just a local string not related to Answer or Option...
  118. Cursor c = sqlite.rawQuery("SELECT " + OptionC + " FROM " + Table_name + " WHERE " + uid + " = " + i + "", null);//cursor to that query
  119. if (c.moveToFirst())
  120. Ans = c.getString(0);
  121. else
  122. Ans = "";
  123. return Ans;
  124. }
  125.  
  126. public String readOptionD(int i)//Used to read the data from the Des.db file where id is given and we choose id randomly
  127. {
  128. String Ans = "";//string that contains the required field note that Ans is just a local string not related to Answer or Option...
  129. Cursor c = sqlite.rawQuery("SELECT " + OptionD + " FROM " + Table_name + " WHERE " + uid + " = " + i + "", null);//cursor to that query
  130. if (c.moveToFirst())
  131. Ans = c.getString(0);
  132. else
  133. Ans = "";
  134. return Ans;
  135. }
  136.  
  137. public String readAnswer(int i)//Used to read the data from the Des.db file where id is given and we choose id randomly
  138. {
  139.  
  140. String Ans = "";//string that contains the required field
  141. Cursor c = sqlite.rawQuery("SELECT " + Answer + " FROM " + Table_name + " WHERE " + uid + " = " + i + "", null);//cursor to that query
  142. if (c.moveToFirst())
  143. Ans = c.getString(0);
  144. else
  145. Ans = "";
  146. return Ans;
  147. }
Add Comment
Please, Sign In to add comment