如何检查数据库是否已锁定

时间:2012-09-05 12:18:35

标签: android sqlite

我在检查数据库是否被锁定时遇到错误。这是我的数据库类。在名为checklocked的方法中检查它是否被锁定。

private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                    + " INTEGER NOT NULL, " + KEY_SYNC + " INTEGER);");

            db.execSQL("CREATE TABLE " + DATABASE_TABLE_2 + " (" + ID_ROW
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + VALUE_ROW
                    + " INTEGER NOT NULL);");

            db.execSQL("CREATE TABLE " + DATABASE_TABLE_3 + " (" + ID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + STATE
                    + " INTEGER NOT NULL);");

            db.execSQL("CREATE TABLE " + DATABASE_TABLE_4 + " (" + ID_CONTACTS
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME
                    + " TEXT NOT NULL, " + NUMBER + " TEXT NOT NULL);");

            db.execSQL("CREATE TABLE " + DATABASE_TABLE_5 + " (" + ID_OFFLINE
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + BODY + " TEXT, "
                    + PHONE + " TEXT, " + DATE + " TEXT, " + M_TYPE + " TEXT, "
                    + CONTACT + " TEXT, " + APP_NAME + " TEXT, " + PACKAGE
                    + " TEXT, " + VERSION + " TEXT," + TYPE + " TEXT, "
                    + DURATION + " TEXT, " + TYPE_CALL + " TEXT);");

            db.execSQL("CREATE TABLE " + DATABASE_TABLE_6 + " (" + ID_CALL_LOG
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ID_CALL_LOG_ID
                    + " INTEGER NOT NULL);");

            db.execSQL("CREATE TABLE " + DATABASE_TABLE_7 + "(" + ID_USED
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ID_USED_YES_NO
                    + " INTEGER);");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE_2);
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE_3);
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE_4);
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE_5);
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE_6);
            onCreate(db);

        }

    }

    public Database(Context c) {
        ourContext = c;

    }

    public Database open() throws SQLException {
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public boolean checklocked() {
        if (ourDatabase.isDbLockedByCurrentThread()) {
            return true;
        } else {
            return false;
        }
    }

    public void close() {
        ourHelper.close();

    }

这段代码给了我这个错误:

09-05 14:13:38.955: E/AndroidRuntime(2262): FATAL EXCEPTION: main
09-05 14:13:38.955: E/AndroidRuntime(2262): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000010 (has extras) } in com.background.Broadcast_Reciver@41647888
09-05 14:13:38.955: E/AndroidRuntime(2262):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at android.os.Handler.handleCallback(Handler.java:605)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at android.os.Looper.loop(Looper.java:137)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at android.app.ActivityThread.main(ActivityThread.java:4507)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at java.lang.reflect.Method.invoke(Method.java:511)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at dalvik.system.NativeStart.main(Native Method)
09-05 14:13:38.955: E/AndroidRuntime(2262): Caused by: java.lang.NullPointerException
09-05 14:13:38.955: E/AndroidRuntime(2262):     at com.background.Database.checkopened(Database.java:139)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at com.background.Broadcast_Reciver.onReceive(BroadcastService.java:100)
09-05 14:13:38.955: E/AndroidRuntime(2262):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
09-05 14:13:38.955: E/AndroidRuntime(2262):     ... 9 more

我已经阅读了google文档,该文档解释了一下这种方法,但没有示例。也许我没有正确使用这种方法。

由于

0 个答案:

没有答案