我的 android 应用程序中的数据库有问题。我在不同数据库的许多地方都遇到了这个异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.logtomobile.gmbclient/com.logtomobile.gmbclient.TransactionHistoryActivity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)
我所有的数据库都使用扩展 SQLiteOpenHelper 的单例类。我也在使用后关闭所有游标。这种异常(exception)的原因可能是什么?我该怎么做才能解决这个问题?
在此异常之前我得到了另一个 SQLite 异常:
(14) cannot open file at line 32473 of [00bb9c9ce4]
(14) os_unix.c:32473: (24) open(/data/data/com.logtomobile.gmbclient/databases/GmbDB-journal)
我无法粘贴我的代码,因为每次都会在代码的不同位置抛出此异常。所有数据库都是在代码中创建的,而不是从外部文件导入的。所有对数据库进行查询的方法都是从 DB Helpers 中的同步方法调用的。在 Helpers 中有静态 Helper 实例(单例),其中也有成员 SQLiteDatabase 对象。这些对象在 Helper 构造函数中由 getWritableDatabase() 初始化一次,并始终保持打开状态而不关闭它们。代码中的每个查询都会在这些 SQLiteDatabase 对象上调用。
public synchronized static GmbDBHelper getInstance(Context context) {
if (sHelper == null) {
sHelper = new GmbDBHelper(context.getApplicationContext());
}
return sHelper;
}
private GmbDBHelper(Context context) {
super(context, GmbDB.DB_NAME, null, DB_VERSION);
mContext = context;
mDatabase = getWritableDatabase();
Log.d("DbHelper", "GmbDbHelper()");
}
synchronized SQLiteDatabase openDbForReading() {
return mDatabase;
}
synchronized SQLiteDatabase openDbForWriting() {
return mDatabase;
}
...
最佳答案
请检查 list 中的权限。并添加以下内容以防您错过。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
关于Android SQLite 数据库异常 - 代码 14(无法打开数据库文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19769490/