Guest User

Sql Error

a guest
Jun 27th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. public class DBHelper extends SQLiteOpenHelper {
  2. private String packageName;
  3. private static final String db_name = "quiz_db.db";
  4. private String db_path;
  5. private static int db_version = 5;
  6. Context con;
  7.  
  8.  
  9. public DBHelper(Context con) {
  10. super(con, db_name, null, db_version);
  11. // TODO Auto-generated constructor stub
  12. this.con = con;
  13. db_path = con.getDatabasePath(db_name).toString().replace(db_name, "");
  14.  
  15. // String filePath = mContext.getDatabasePath(Utils.getDatabaseName()).getAbsolutePath();
  16.  
  17. //String filePath = mContext.getDatabasePath(Utils.getDatabaseName()).getAbsolutePath();
  18.  
  19. }
  20.  
  21. @Override
  22. public void onCreate(SQLiteDatabase db) {
  23. // TODO Auto-generated method stub
  24.  
  25. }
  26.  
  27. public void createDB() throws IOException {
  28.  
  29. if (checkDB()) {
  30. } else if (!checkDB()) {
  31. this.getReadableDatabase();
  32. copyDB();
  33. }
  34.  
  35. // if (checkDB()) {
  36. //
  37. // } else {
  38. // this.getReadableDatabase();
  39. // copyDB();
  40. // }
  41.  
  42. }
  43.  
  44. private boolean checkDB() {
  45.  
  46. SQLiteDatabase cDB = null;
  47. try {
  48. cDB = SQLiteDatabase.openDatabase(db_path + db_name, null,
  49. SQLiteDatabase.OPEN_READWRITE);
  50. } catch (SQLiteException e) {
  51. e.printStackTrace();
  52. }
  53. if (cDB != null) {
  54. cDB.close();
  55. }
  56. return cDB != null ? true : false;
  57. }
  58.  
  59.  
  60. private void copyDB() throws IOException {
  61. InputStream inputFile = con.getAssets().open(db_name);
  62. String outFileName = db_path + db_name;
  63. OutputStream outFile = new FileOutputStream(outFileName);
  64. byte[] buffer = new byte[1024];
  65. int length;
  66. while ((length = inputFile.read(buffer)) > 0) {
  67. outFile.write(buffer, 0, length);
  68. }
  69. outFile.flush();
  70. outFile.close();
  71. inputFile.close();
  72. }
  73.  
  74. @Override
  75. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  76. // TODO Auto-generated method stub
  77.  
  78. if(newVersion>oldVersion) {
  79. try {
  80. copyDB();
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85.  
  86.  
  87.  
  88. }
  89.  
  90. public List<Quizplay> getQuestionGuj(int noOfQuestion, int level) {
  91. List<Quizplay> quizplay = new ArrayList<Quizplay>();
  92. int total = noOfQuestion;
  93. String sql = "select * FROM questions_list where level=" + level + " ORDER BY RANDOM() LIMIT " + total;
  94. SQLiteDatabase db = this.getReadableDatabase();
  95. //SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/" + packageName + "/databases/" + DATABASE_NAME, null, 0);
  96. Cursor cursor = db.rawQuery(sql, null);
  97. int i = 1;
  98. if (cursor.moveToFirst()) {
  99. do {
  100. Quizplay question = new Quizplay(cursor.getString(cursor.getColumnIndex("question")));
  101. question.addOption(cursor.getString(cursor.getColumnIndex("option_a")));
  102. question.addOption(cursor.getString(cursor.getColumnIndex("option_b")));
  103. question.addOption(cursor.getString(cursor.getColumnIndex("option_c")));
  104. question.addOption(cursor.getString(cursor.getColumnIndex("option_d")));
  105. String rightAns = cursor.getString(cursor.getColumnIndex("right_answer"));
  106. System.out.println("right ans " + rightAns);
  107. if (rightAns.equalsIgnoreCase("A")) {
  108. question.setTrueAns(cursor.getString(cursor.getColumnIndex("option_a")));
  109. } else if (rightAns.equalsIgnoreCase("B")) {
  110. question.setTrueAns(cursor.getString(cursor.getColumnIndex("option_b")));
  111. } else if (rightAns.equalsIgnoreCase("C")) {
  112. question.setTrueAns(cursor.getString(cursor.getColumnIndex("option_c")));
  113. } else {
  114. question.setTrueAns(cursor.getString(cursor.getColumnIndex("option_d")));
  115. }
  116. if (question.getOptions().size() == 4) {
  117. quizplay.add(question);
  118. }
  119. i++;
  120. } while (cursor.moveToNext());
  121. }
  122. cursor.close();
  123. db.close();
  124. Collections.shuffle(quizplay);
  125. quizplay = quizplay.subList(0, noOfQuestion);
  126. return quizplay;
  127. }
Add Comment
Please, Sign In to add comment