问题描述
我试图打开一个SQLiteDatabase一个服务
它失败,出现以下异常:
I am trying to open a SQLiteDatabase in a "Service"
which fails with the following exception:
10-21 19:33:23.547: E/SqliteDatabaseCpp(31208): sqlite3_exec - Failed to set synchronous mode = 1(Normal)
10-21 19:33:23.547: E/SQLiteDatabase(31208): Failed to open the database. closing it.
10-21 19:33:23.547: E/SQLiteDatabase(31208): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1135)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1086)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1173)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:858)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:228)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
有人能请解释一下这是什么意思?以及如何可以把它固定?数据库在打开 MainActivity
和服务
。
推荐答案
我觉得这是一个访问冲突发生时使用(锁定)由一个线程(活动),另一个线程分贝(服务?)试图用(锁),但失败了。
I think it is an access conflict happened when the db was used (locked) by one thread (the activity?), the other thread (the service?) tried to use (lock) it but failed.
您可以使自己的SQLiteOpenHelper一个单例类,然后活动和服务之间共享,然后类将妥善处理好冲突为您服务。见the在这个线程讨论
You can make your own SQLiteOpenHelper a singleton class, and then share it between the activity and service, and then the class will handle the conflict properly for you. See the discussion in this thread
这篇关于这是什么错误讯息是什么意思? - sqlite3_exec - 无法设置同步模式= 1(正常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!