没有这样的表:引号(代码1 SQLITE_ERROR):Android P

时间:2018-09-29 10:45:30

标签: java android sqlite

我有一个报价应用程序,其中使用SqlLite数据库进行报价。当用户打开应用程序时,其将数据从资产复制到数据库。在所有版本中都能正常工作,但在Android P中,它在应用程序启动时崩溃。

它给我类似

的错误
android.database.sqlite.SQLiteException: no such table: quotes (code 1 SQLITE_ERROR): , while compiling: SELECT COUNT(qu_text) AS count FROM quotes

我确定数据库不会被复制,因此不会出现表错误,但仅适用于AndroidP。其他版本也可以正常工作。

我的DBHandler.java如下所示

public class DBHandler extends SQLiteOpenHelper {
    private static String DB_PATH;
    private static String DB_NAME = "xxx";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    DBHandler(Context context) {

        super(context, DB_NAME, null, constant.DATABASE_VERSION);
        this.myContext = context;
        DB_PATH = context.getDatabasePath(DB_NAME).toString();
    }
    void createDataBase() throws IOException {

        boolean dbExist = checkDataBase();

        if (dbExist) {
            Log.e("database","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;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {

            // database does't exist yet.

        }

        if (checkDB != null) {

            checkDB.close();

        }

        return checkDB != null;
    }


    private void copyDataBase() throws IOException {

        InputStream myInput = myContext.getAssets().open(DB_NAME);

        String outFileName = DB_PATH;

        OutputStream myOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    // ==============================================================================

    void openDataBase() throws SQLException {

        String myPath = DB_PATH;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }


    // ==============================================================================

    @Override
    public synchronized void close() {

        if (myDataBase != null)
            myDataBase.close();

        super.close();

    }

    // ==============================================================================

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    // ==============================================================================

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

DAO.java类具有如下功能

private void open() throws SQLException {
    if(database!=null&&database.isOpen())return;
    database = dbHandler.getWritableDatabase();

}


private void closeDAO(){
    synchronized (lockObj){
        if(dbHandler!=null)dbHandler.close();
        dbHandler=null;
        database=null;
    }
}



public static void dispose(){

    if(dBObject!=null){
        dBObject.closeDAO();
    }
    dBObject=null;
}

我曾尝试在复制数据库之前关闭数据库连接,如下图所示,因为一位用户回答说,但是仍然出现相同的错误。

private void copyDataBase() throws IOException {
        SQLiteDatabase db = getReadableDatabase();
        db.close();

        InputStream myInput = myContext.getAssets().open(DB_NAME);

        String outFileName = DB_PATH;

        OutputStream myOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

让我知道是否有人可以帮助解决难题。谢谢

1 个答案:

答案 0 :(得分:0)

这是Android P中的预期行为。您可以在https://issuetracker.google.com/issues/80268903

中阅读详细信息

要解决此问题,应用程序必须在复制文件之前确保没有打开的数据库连接。