- mDatabase.beginTransaction();
- try
- {
- Log.v(TAG,"locked:"+mDatabase.isDbLockedByCurrentThread());
-
- mDatabase.execSQL(sql);
- mDatabase.setTransactionSuccessful();
- }
- finally
- {
- mDatabase.endTransaction();
- }
原因是数据库被锁定了。
在每次操作数据库之前(一般是增删改对数据库进行写操作时受影响,query不影响),检查锁定状态。
每次操作数据库,按下面较完整的操作进行
- private static String Lock = "dblock";
- private static void executeSQL(final String sql)
- {
- SQLiteDatabase db = getDatabase();
- synchronized (Lock)
- {
- db.beginTransaction();
- try
- {
- db.execSQL(sql);
- db.setTransactionSuccessful();
- }
- finally
- {
- db.endTransaction();
- }
- }
- }
- private static SQLiteDatabase getDatabase()
- {
- while (mDatabase.isDbLockedByCurrentThread()
- || mDatabase.isDbLockedByOtherThreads())
- {
-
-
- }
- return mDatabase;
- }
- <span style="white-space:pre"></span><pre></pre>
- <pre></pre>
-