gpt4 book ai didi

Android如何检查数据库是否存在

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:19 24 4
gpt4 key购买 nike

我想检查数据库是否存在,我从网上找到了一些代码。

     private boolean checkDataBase(){

SQLiteDatabase checkDB = null;

try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);

}catch(SQLiteException e){

//database does't exist yet.

}

if(checkDB != null){

checkDB.close();

}

return checkDB != null ? true : false;
}

但是我得到一个错误..无法打开文件为什么它不能工作?

: sqlite returned: error code = 14, msg = cannot open file at line 27703 of [8609a15dfa], db=/data/data/com.testing.abcde/databases/scj
: sqlite returned: error code = 14, msg = os_unix.c: open() at line 27703 - "" errno=2 path=/data/data/com.testing.abcde/databases/scj, db=/data/data/com.testing.abcde/databases/scj
: sqlite3_open_v2("/data/data/com.testing.abcde/databases/scj", &handle, 1, NULL) failed
: Failed to open the database. closing it.
: android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
: at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1013)
: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
: at com.lkland.softkeyboard.easytype.SQLite.checkDataBase(SQLite.java:80)
: at com.lkland.softkeyboard.easytype.SQLite.createDataBase(SQLite.java:55)

最佳答案

我使用这段代码,它似乎运行良好:

// Where DB_PATH is /data/data/com.testing.abcde/databases/scj
// and DB_NAME is name.db
public boolean checkDBExists() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}

您将需要import java.io.File; 或提供对 File 类的完整引用。

关于Android如何检查数据库是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11657215/

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