Android Sqlite提取数据不可能

时间:2012-07-09 10:32:31

标签: android sqlite cursor

我只是在阅读我的桌子,但我无法启动应用程序,因为它给了我这些错误:

这是连接到dbclass的代码。它在DBclass中调用id方法,然后关闭db。

public int return_id_phone(Context contxt) {
    Database db = new Database(contxt);
    db.open();
    int id_phone = db.id(2);
    db.close();
    return id_phone;
}

这是要提取的代码。只返回一个简单的数字,我不知道它为什么会崩溃。它在下面的错误中说我不关闭游标,而且db也是锁定。

public int id(int row) {
    String[] columns = new String[] { ID_ROW, VALUE_ROW };
    Cursor c = ourDatabase.query(DATABASE_TABLE_2, columns, ID_ROW + "='"
            + row+"'", null, null, null, null);
    if (c != null) {
        c.moveToFirst();
        int id= c.getInt(1);
        c.close();
        return id;
    } else {
        c.close();
        return 0;
    }
}

我不知道我做错了什么......

> 07-09 11:54:05.460: E/SQLiteDatabase(6938): close() was never explicitly called on database '/data/data/com.anonyme.app/databases/faraway' 
07-09 11:54:05.460: E/SQLiteDatabase(6938): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:2069)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1123)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1081)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1167)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:833)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at com.background.Database.open(Database.java:89)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at com.extract.Contacts.phoneandname(Contacts.java:124)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at com.anonyme.app.IntroActivity.uploadall(IntroActivity.java:85)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at com.anonyme.app.IntroActivity.onCreate(IntroActivity.java:76)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.app.Activity.performCreate(Activity.java:4465)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.os.Looper.loop(Looper.java:137)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at android.app.ActivityThread.main(ActivityThread.java:4507)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at java.lang.reflect.Method.invoke(Method.java:511)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-09 11:54:05.460: E/SQLiteDatabase(6938):     at dalvik.system.NativeStart.main(Native Method)
07-09 11:54:05.460: E/System(6938): Uncaught exception thrown by finalizer
07-09 11:54:05.465: E/System(6938): java.lang.IllegalStateException: Don't have database lock!
07-09 11:54:05.465: E/System(6938):     at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2227)
07-09 11:54:05.465: E/System(6938):     at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2319)
07-09 11:54:05.465: E/System(6938):     at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2315)
07-09 11:54:05.465: E/System(6938):     at android.util.LruCache.trimToSize(LruCache.java:197)
07-09 11:54:05.465: E/System(6938):     at android.util.LruCache.evictAll(LruCache.java:285)
07-09 11:54:05.465: E/System(6938):     at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2280)
07-09 11:54:05.465: E/System(6938):     at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1252)
07-09 11:54:05.465: E/System(6938):     at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:2040)
07-09 11:54:05.465: E/System(6938):     at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
07-09 11:54:05.465: E/System(6938):     at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
07-09 11:54:05.465: E/System(6938):     at java.lang.Thread.run(Thread.java:856)

2 个答案:

答案 0 :(得分:0)

尝试以下代码:

public int id(int row) {
    String[] columns = new String[] { ID_ROW, VALUE_ROW };
    try {
        Cursor c = ourDatabase.query(DATABASE_TABLE_2, columns, ID_ROW + "='"
            + row+"'", null, null, null, null);
        if (c != null) {
            c.moveToFirst();
            int id= c.getInt(1);
            return id;
        } else {
             return 0;
        }
    } finally {
        c.close();
    }
}

答案 1 :(得分:0)

我看到了一些事情......首先,您在查询中将整数视为字符串(删除引号以解决此问题)。

其次,游标可以为空而不为空,因此您应该更改检查以查找0记录计数。

试试这个:

public int id(int row) { 
    String[] columns = new String[] { ID_ROW, VALUE_ROW }; 
    Cursor c = ourDatabase.query(DATABASE_TABLE_2, columns, ID_ROW + "= " 
        + row, null, null, null, null); 
    if (c.getcount() != 0) { 
        c.moveToFirst(); 
        int id= c.getInt(1); 
        c.close(); 
        return id; 
    } else { 
        c.close(); 
        return 0; 
    } 
}