sqliteexception:没有这样的表:问题列表(代码1 sqlite\u错误)

iszxjhcz  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(381)

我正在创建一个琐事应用程序,其中所有的问题都是从sqlitedb加载的,表名为“questions\u list”。
在android 8(oreo)上进行测试时一切正常,但当我构建apk并在android 9(redmi)上运行时,它会崩溃并出现以下错误:完整代码: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

这是我的密码:

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;

如何修复此错误?请引导

yuvru6vn

yuvru6vn1#

checkdb方法中的逻辑必须纠正如下:

public void createDB() throws IOException {

    if (checkDB()) {

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

}

相关问题