【问题标题】:SQLiteException: no such table: questions_list (code 1 SQLITE_ERROR)SQLiteException:没有这样的表:questions_list(代码 1 SQLITE_ERROR)
【发布时间】:2020-10-17 20:25:04
【问题描述】:

我正在创建一个 Trivia 应用程序,其中所有问题都从 SQlite DB 加载,表名为“questions_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;

如何修复此错误?请指导

【问题讨论】:

标签: android sql sqlite datatable android-sqlite


【解决方案1】:

checkDB 方法中的逻辑必须更正如下:

public void createDB() throws IOException {

    if (checkDB()) {

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

}

【讨论】:

  • 不工作先生,刚刚更换和检查,仍然是同样的问题。 @dev242
  • 你能分享资产文件以便我调试吗? (或将 github 上的代码推送到私人存储库中并邀请我:Steve111MV on Github
  • 这里是完整的代码:pastebin.com/CLNxcEFJ
  • 你也可以上传 questions_list 文件吗?有 5 个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多