gpt4 book ai didi

android - SQLite 无法在频繁的 "SELECT"查询中打开数据库文件(代码 14)

转载 作者:IT王子 更新时间:2023-10-29 06:23:08 26 4
gpt4 key购买 nike

我有以下类“Singleton”来处理 SQLite 连接并确保整个进程/应用程序有 1 个连接实例:

public class DBController {
private static DBController instance = new DBController();
private static DBHelper dbHelper;
public static DBController getInstance()
{
return instance;
}
public SQLiteDatabase dbOpen(Context context)
{
if(dbHelper == null)
dbHelper = new DBHelper(context);
return dbHelper.getWritableDatabase();
}
}

和 DBHelper 类本身:

public class DBHelper extends SQLiteOpenHelper {

public DBHelper(Context context) {
super(context, "database.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
final String position = "CREATE TABLE test (" +
"test TEXT NOT NULL);";
db.execSQL(position);
}
}

当我经常尝试从数据库中“选择”一些信息时,我收到以下错误:

SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
SQLiteLog: (14) os_unix.c:31278: (24) open(/data/user/0/uz.mycompany.myapp/databases/database.db-journal) -
SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
SQLiteLog: (14) os_unix.c:31278: (24) open(/data/user/0/uz.mycompany.myapp/databases/database.db-journal) -
SQLiteLog: (14) statement aborts at 29: [SELECT * FROM test WHERE test='testdata1'] unable to open database file
SQLiteQuery: exception: unable to open database file (code 14); query: SELECT * FROM test WHERE test='testdata1'
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)

我正在运行以下代码来执行查询:

public String getData(Context context)
{
SQLiteDatabase _db = dbOpen(context);
Cursor c = _db.rawQuery("SELECT * FROM test WHERE test='testdata1'", null);
return getDataFromCursor(c).get(0); //gets data from cursor and returns first one
}

我如何管理/改进我的数据库连接以克服/避免这个问题?

最佳答案

在对这些东西执行任何查询之前(你应该打开你的数据库)。完成任务后关闭数据库。

private DBHelper dbHelper = new DBHelper(context);

try {
_db = dbHelper.getWritableDatabase();
} catch (SQLException s) {
new Exception("Error with DB Open");
}

//然后现在写你的查询....然后关闭数据库。

_db.close();

你可以看看我的git link1 git link2

关于android - SQLite 无法在频繁的 "SELECT"查询中打开数据库文件(代码 14),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35008632/

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