SQLite:没有这样的表错误Android P问题

aiazj4mn  于 2022-12-21  发布在  Android
关注(0)|答案(5)|浏览(149)

在Android P之前,我的代码一切正常,但现在在Android P中,我在使用数据库时遇到了很多问题。
我有.db SQLite数据库文件存储在资产文件夹中。我正在将它们导入Android的数据库空间,直到Android P它工作正常。在Android P中,我得到这个异常:第一个月
我在这里搜索并找到了这个,我试图应用公认的答案:安卓P - 'SQLite:从资产复制数据库后出现“无此类表错误
问题是,现在我有另一个问题,它仍然不工作。现在我有问题,每次我试图检查数据库是否已经存在,这段代码总是返回真,所以我从来没有创建数据库。即使数据库没有创建,即使应用程序最近安装:

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

我的源代码是前面引用的堆栈溢出问题中被接受答案的源代码。
新的Android P需要获取DB路径的方式:

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类:

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();
    }
}
rkttyhzu

rkttyhzu1#

Arg在this.getReadableDatabase();之后添加this.close();最终解决了该问题
该打开的连接正在生成原始的no such table异常
所以我使用了Util.getDatabasePath(DB_NAME);而不是Android P中提出的复杂解决方案-“SQLite:从资产复制数据库后出现“无此类表错误”,现在代码更简单
非常感谢@生活方式,他们发现了真实的的问题。
现在代码简单多了:

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();
    }
}
ifsvaxew

ifsvaxew2#

在DBHelper类中,您将在测试方法Util.getDatabasePath(DB_NAME)中是否存在DB之前创建DB。

this.DB_COMPLETE_PATH = Util.getDatabasePath(DB_NAME);

通过调用此方法,SQL打开帮助器创建数据库(在本例中为空,因为在OnCreate()方法中没有发生任何事情)。
如果您将数据库存储在默认应用程序的数据库路径中,则可以通过下一个代码段检查数据库是否存在:

File f = context.getDatabasePath("my_database.db");
if(f.exists()) {
   //Your code if DB exist
} else {
   //Your code if DB doesn't exist
}

禁用WAL的更好位置是SQLiteOpenHelper中的onConfigure()方法

ddrv8njm

ddrv8njm3#

将此路径添加到数据库

String dbPath = "/data/data/" + mContext.getPackageName() + "/cache/" + mDataBaseName;
eoxn13cs

eoxn13cs4#

事实上,我已经把SQLiteDatabase的对象作为静态的,所以我得到了这个问题。

polkgigr

polkgigr5#

我的Android P问题通过在this.getReadableDatabase();之后添加this.close();得到了解决

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

[https://stackoverflow.com/a/53258585/4657260][1]

相关问题