没有此类表错误Android

没有此类表错误Android

本文介绍了SQLite:没有此类表错误Android P问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到Android P,所有代码都可以正常运行,但是现在在Android P中,使用数据库时会遇到很多问题.

Until Android P everything worked fine with my code but now in Android P I'm having a lot of problems working with databases.

我在Assets文件夹中存储了.db sqlite数据库文件.我将它们导入到android的数据库空间中,直到Android P都可以正常工作.在android P中,我遇到了以下异常:SQLite: No Such Table Error

I have .db sqlite database files stored on assets folder. I was importing them into the database space of android and it worked fine until Android P. In android P I'm getting this exception: SQLite: No Such Table Error

我在这里搜索并找到了这个,然后尝试应用被接受的答案: Android P-'从资产复制数据库后,SQLite:没有这样的表错误"

I searched here and found this, and I tryed to apply the accepted answer:Android P - 'SQLite: No Such Table Error' after copying database from assets

问题是,现在我遇到了另一个问题,但仍然无法正常工作.现在,我遇到的问题是,每次尝试检查数据库是否已存在时,此代码始终返回true,所以我从不创建数据库.即使未创建数据库,即使最近安装了应用程序,也是如此:

The problem is that now I'm having another problem and It still doesn't works. Now I'm having the problem that everytime I try to check if the database already exists, this code always returns true, so I never create the database. Even with the database not created, even with the app recently installed:

private boolean checkIfDBExists() {
    File dbFile = new File(DB_COMPLETE_PATH);
    return dbFile.exists();
}

我的源代码是前面引用的stackoverflow问题中接受的答案的源代码.

My source code is the source code of the accepted answer in the previously quoted stackoverflow question.

新的Android P需要获取数据库路径的方法:

The new Android P required way of getting the DB path:

public static String getDatabasePath(String fileNname){
    MySQLiteOpenHelper helper = new MySQLiteOpenHelper(ApplicationContextProvider.getContext(), fileNname);
    SQLiteDatabase database = helper.getReadableDatabase();
    String path = database.getPath();
    database.close();
    return path;
}

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    public MySQLiteOpenHelper(Context context, String databaseName) {
        super(context, databaseName, null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        db.disableWriteAheadLogging();
    }
}

我的DBHelper类:

My DBHelper class:

public class DbHelper extends SQLiteOpenHelper{
    private  String DB_NAME;
    private  String DB_COMPLETE_PATH;
    private SQLiteDatabase mDataBase;
    private static final int DATABASE_VERSION = 1;

    public DbHelper(String name) throws IOException {
        super(ApplicationContextProvider.getContext(), name+".db", null, DATABASE_VERSION);
        this.DB_NAME = name+".db";
        this.DB_COMPLETE_PATH = Util.getDatabasePath(DB_NAME);

        boolean mustOverWriteDB;

        if (checkIfDBExists()==false) {
            createDataBase();
        }

        openDataBase();
    }

    private void createDataBase() throws IOException {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void copyDataBase() throws IOException {
        InputStream myInput = App.getInstance().getAssetsFile(DB_NAME);
        String outFileName = DB_COMPLETE_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();
    }
}

推荐答案

Arg终于解决了它,在this.getReadableDatabase();

Arg finally solved it adding this.close(); after this.getReadableDatabase();

打开的连接正在生成原始的无此类表异常

That openned connection was generating the original no such table exception

所以我用了Util.getDatabasePath(DB_NAME);而不是,现在的代码要简单得多

So I used Util.getDatabasePath(DB_NAME); instead of the complex propossed solution in Android P - 'SQLite: No Such Table Error' after copying database from assets and now the code is much more simpler

非常感谢@LifeStyle发现了真正的问题.

Thanks a lot to @LifeStyle who found the real problem.

现在,代码更加简单:

public static String getDatabasePath(String fileNname){
    return ApplicationContextProvider.getContext().getDatabasePath(fileNname).getAbsolutePath();
}

public class DbHelper extends SQLiteOpenHelper{
    private String DB_NAME;
    private String DB_COMPLETE_PATH;
    private SQLiteDatabase mDataBase;
    private static final int DATABASE_VERSION = 1;

    public DbHelper(String name) throws IOException {
        super(ApplicationContextProvider.getContext(), name+".db", null, DATABASE_VERSION);
        this.DB_NAME = name+".db";
        this.DB_COMPLETE_PATH = Util.getDatabasePath(DB_NAME);

        if (checkIfDBExists()==false){
            createDataBase();
        }

        openDataBase();
    }

    private void createDataBase() throws IOException {
        this.getReadableDatabase();
        this.close();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private boolean checkIfDBExists() {
        File dbFile = new File(DB_COMPLETE_PATH);
        return dbFile.exists();
    }
}

这篇关于SQLite:没有此类表错误Android P问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:33