Android-andorid sqlite数据库连接异常
Android系统支持sqlite数据库,但我在使用Android系统访问sqlite数据库进行调试程序时,有时可能会遇到android.database.sqlite.SQLiteException: database is locked这个异常。
06-29 13:17:12.415: E/Database(10064): CREATE TABLE android_metadata failed
06-29 13:17:12.455: E/Database(10064): Failed to setLocale() when constructing, closing the database
06-29 13:17:12.455: E/Database(10064): android.database.sqlite.SQLiteException: database is locked
06-29 13:17:12.455: E/Database(10064): at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
06-29 13:17:12.455: E/Database(10064): at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1987)
06-29 13:17:12.455: E/Database(10064): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1855)
06-29 13:17:12.455: E/Database(10064): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看这段代码,SQLiteDatabase的构造函数:
/**
* Private constructor. See {@link #create} and {@link #openDatabase}.
*
* @param path The full path to the database
* @param factory The factory to use when creating cursors, may be NULL.
* @param flags 0 or {@link #NO_LOCALIZED_COLLATORS}. If the database file already
* exists, mFlags will be updated appropriately.
*/
private SQLiteDatabase(String path, CursorFactory factory, int flags) {
if (path == null) {
throw new IllegalArgumentException("path should not be null");
}
mFlags = flags;
mPath = path;
mSlowQueryThreshold = SystemProperties.getInt(LOG_SLOW_QUERIES_PROPERTY, -1);
mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace();
mFactory = factory;
dbopen(mPath, mFlags);
if (SQLiteDebug.DEBUG_SQL_CACHE) {
mTimeOpened = getTime();
}
mPrograms = new WeakHashMap<SQLiteClosable,Object>();
try {
setLocale(Locale.getDefault());
} catch (RuntimeException e) {
Log.e(TAG, "Failed to setLocale() when constructing, closing the database", e);
dbclose();
if (SQLiteDebug.DEBUG_SQL_CACHE) {
mTimeClosed = getTime();
}
throw e;
}
}
当setLocale失败的时候会被cache,所以出现Failed to setLocale() when constructing, closing the database的异常.
消除这个问题就是设置参数flag为NO_LOCALIZED_COLLATORS,参考上面的注释。