首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >仅在Android P(8.1)仿真器中出错]:android.database.sqlite.SQLiteException:没有这样的表:(代码1 SQLITE_ERROR):

仅在Android P(8.1)仿真器中出错]:android.database.sqlite.SQLiteException:没有这样的表:(代码1 SQLITE_ERROR):
EN

Stack Overflow用户
提问于 2018-05-01 20:24:27
回答 1查看 810关注 0票数 3

我在assert/ folder中有一个数据库文件,并且我使用代码来复制数据库。但将其复制到设备后,表就丢失了。这只有当我在Android P(8.1)中运行应用程序时才会发生,它与其他android版本一起工作得很好,我在Nougat上也检查过,它工作得很好。

如果Android P有任何新的更新,请帮助。??

复制数据库代码:

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        //do nothing - database already exist            
    } else {

             this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");
        }
    }
}

private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {
        //database does't exist yet.
        e.printStackTrace();
    }

    if (checkDB != null) {

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50116413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档