Android-andorid sqlite数据库连接异常

WP主题讨论 WP主题讨论 主题:1013 回复:2239

Android-andorid sqlite数据库连接异常

瑾兮 发布于 2016-10-23 字数 951 浏览 1207 回复 1

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)

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

支持 Markdown 语法,需要帮助?

评论(1

瑾兮 2017-10-24 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,参考上面的注释。