0

I am creating a Trivia App, where all the Questions are loaded from SQlite DB with table name "questions_list".

Everything is working fine in Testing on Android 8 (Oreo), but when I build the Apk and run it on Android 9 (Redmi), it crashes with the following error : Complete code : https://pastebin.com/CLNxcEFJ

android.database.sqlite.SQLiteException: no such table: questions_list (code 1 SQLITE_ERROR): , while compiling: select *  FROM questions_list   where level=1 ORDER BY RANDOM() LIMIT 10

Here is my Code :

public DBHelper(Context con) {
    super(con, db_name, null, db_version);
    // TODO Auto-generated constructor stub
    this.con = con;
    db_path = con.getDatabasePath(db_name).toString().replace(db_name, "");

}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

public void createDB() throws IOException {

    if (checkDB()) {
    } else if (!checkDB()) {
        this.getReadableDatabase();
        copyDB();
    }

}

private boolean checkDB() {

    SQLiteDatabase cDB = null;
    try {
        cDB = SQLiteDatabase.openDatabase(db_path + db_name, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
        e.printStackTrace();
    }
    if (cDB != null) {
        cDB.close();
    }
    return cDB != null ? true : false;
}


private void copyDB() throws IOException {
    InputStream inputFile = con.getAssets().open(db_name);
    String outFileName = db_path + db_name;
    OutputStream outFile = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputFile.read(buffer)) > 0) {
        outFile.write(buffer, 0, length);
    }
    outFile.flush();
    outFile.close();
    inputFile.close();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub


}

public List<Quizplay> getQuestionGuj(int noOfQuestion, int level) {
    List<Quizplay> quizplay = new ArrayList<Quizplay>();
    int total = noOfQuestion;
    String sql = "select *  FROM questions_list   where level=" + level + " ORDER BY RANDOM() LIMIT " + total;
    SQLiteDatabase db = this.getReadableDatabase();
    //SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/" + packageName + "/databases/" + DATABASE_NAME, null, 0);
    Cursor cursor = db.rawQuery(sql, null);
  
    if (cursor.moveToFirst()) {
        do {
            Quizplay question = new Quizplay(cursor.getString(cursor.getColumnIndex("question")));
            question.addOption(cursor.getString(cursor.getColumnIndex("option_a")));
            
       
        } while (cursor.moveToNext());
    }
   
    return quizplay;

How to Fix this Error ? Please Guide

ashish
  • 63
  • 7

1 Answers1

0

You logic in the checkDB method has to be corrected as below:

public void createDB() throws IOException {

    if (checkDB()) {

    } else { 
        this.getReadableDatabase();
        copyDB();
    }

}
Steve NDENDE
  • 19
  • 1
  • 9