gpt4 book ai didi

android - 获取 SQLiteCantOpenDatabaseException

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:15 26 4
gpt4 key购买 nike

在我的应用程序中,我的 Assets 文件夹中有一个 SQLite 数据库的副本。据我所知,它工作正常。当我的应用程序第一次在我的模拟器中安装时,我收到如下错误:

Failed to open the database. Closing it.

android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
E/SQLiteDatabase(7516): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
E/SQLiteDatabase(29308): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1013)
E/SQLiteDatabase(29308): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
E/SQLiteDatabase(29308): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
E/SQLiteDatabase(29308): at com.guayama.database.URLDatabaseHelper.checkDBExists(URLDatabaseHelper.java:86)
E/SQLiteDatabase(29308): at com.guayama.database.URLDatabaseHelper.createURLDB(URLDatabaseHelper.java:54)
E/SQLiteDatabase(29308): at com.guayama.database.URLDatabaseHelper.<init>(URLDatabaseHelper.java:38)

这是我打开数据库的初始代码:

 SQLiteDatabase.openDatabase(mPath, null,SQLiteDatabase.CREATE_IF_NECESSARY);

这是我尝试过的另一种方法:

checkDB = SQLiteDatabase.openDatabase(mPath, null,
SQLiteDatabase.OPEN_READONLY);

我也试过用这个:

 SQLiteDatabase.openDatabase(mPath, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);

关于如何解决这个问题有什么建议吗?

最佳答案

我总是按照以下步骤从 Assets 文件夹复制我的数据库:

  private void copyDatabase() throws IOException{

InputStream inputStream = context.getAssets().open(DB_NAME);
String dbCreatePath = DB_PATH+DB_NAME;
OutputStream outputStream = new FileOutputStream(dbCreatePath);
byte[] buffer = new byte[1024];
int length;
while((length = inputStream.read(buffer))>0){
outputStream.write(buffer,0,length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}

一旦它被复制,我将检查如下:

  private boolean checkDatabase(){
SQLiteDatabase checkDB = null;
try {
String dbPath = DB_PATH+DB_NAME;
checkDB = SQLiteDatabase.openDatabase(dbPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// TODO: handle exception
}
if(checkDB!=null){
checkDB.close();}
return checkDB != null ? true:false;

}

关于android - 获取 SQLiteCantOpenDatabaseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11157049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com