打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

直到Android P一切都运行良好我的代码,但现在在Android P我在使用数据库时遇到了很多问题.

我有.db sqlite数据库文件存储在assets文件夹中.我正在将它们导入android的数据库空间,它工作正常,直到Android P.在android P中我得到这个异常:SQLite:没有这样的表错误

我在这里搜索并找到了这个,我试着应用接受的答案:
Android P – ‘SQLite: No Such Table Error’ after copying database from assets

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

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

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

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

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

解决方法:

Arg最终解决了它添加this.close();在this.getReadableDatabase()之后;

那个打开的连接正在生成原始的没有这样的表异常

所以我使用了Util.getDatabasePath(DB_NAME);而不是Android P – ‘SQLite: No Such Table Error’ after copying database from assets中复杂的提议解决方案,现在代码更简单

非常感谢@LifeStyle,感谢他,我找到了真正的问题

现在代码更简单了:

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();    }}
来源:http://www.icode9.com/content-2-238501.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
什么是一个完整的Android数据库的Helper类为现有的SQLite数据库?
android SQLite数据库在Android中的API
android中的数据库操作
sqlite使用详解
Android 小项目之
How To Android SQLite onUpgrade() | BHW Blog
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服