0
public Cursor getsomething()
{
    this.open(); 
    Cursor c= database.query(DatabaseHandler.Table_Name,magicdata,null, null, null,null,null); 

    this.close();
    return c;
}

我正在使用此代码来查询我的数据库。然后我在主要活动中调用它。

get=X.getsomething();
if (get.getCount() > 0)
{
    if  (get.moveToFirst())
    {
        do {
        String uname = get.getString(get.getColumnIndex("content_id"));
    String unknown =url + uname;
    String ax= get.getString(get.getColumnIndex("content"));
    downloadimages(url,ax); 
        }
        while (get.moveToNext());
    }
}
}} 

我在error这里得到一个,我的代码没有运行,我不知道为什么?

错误代码如下

 02-20 10:27:18.378: E/PhonePolicy(1261): Could not preload class for phone policy com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
 02-20 10:27:18.488: D/TextLayoutCache(1261): Using debug level: 0 - Debug Enabled: 0
 02-20 10:27:20.998: D/AndroidRuntime(1261): Shutting down VM
 02-20 10:27:20.998: W/dalvikvm(1261): threadid=1: thread exiting with uncaught exception    (group=0x40a3e1f8)
 02-20 10:27:20.998: E/AndroidRuntime(1261): FATAL EXCEPTION: main
 02-20 10:27:20.998: E/AndroidRuntime(1261): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.newpicker/com.example.newpicker.MainActivity}: java.lang.IllegalStateException: database /data/data/com.example.newpicker/databases/content.db (conn# 0) already closed
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.os.Handler.dispatchMessage(Handler.java:99)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.os.Looper.loop(Looper.java:137)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.app.ActivityThread.main(ActivityThread.java:4429)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at java.lang.reflect.Method.invokeNative(Native Method)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at java.lang.reflect.Method.invoke(Method.java:511)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at dalvik.system.NativeStart.main(Native Method)
 02-20 10:27:20.998: E/AndroidRuntime(1261): Caused by: java.lang.IllegalStateException: database /data/data/com.example.newpicker/databases/content.db (conn# 0) already closed
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:413)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:400)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:79)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at com.example.newpicker.MainActivity.datamining(MainActivity.java:70)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at com.example.newpicker.MainActivity.onCreate(MainActivity.java:45)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.app.Activity.performCreate(Activity.java:4465)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
 02-20 10:27:20.998: E/AndroidRuntime(1261):    ... 11 more
4

2 回答 2

1

这是您的解决方案。尝试这个。

String[] columnArray = { "content_id, content " };
        Cursor cursor = database.query(DatabaseHelper.TABLE_NAME,columnArray, null, null, null, null, null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            do {
                int id = cursor.getInt(cursor.getColumnIndex("content_id"));
                String content =cursor.getString(cursor.getColumnIndex("content"));
            } while (cursor.moveToNext());
            cursor.close();
        } else {
            Log.i("TAG"," No value found");

        }
于 2013-02-19T12:28:16.443 回答
1

阅读线

02-20 10:27:20.998: E/AndroidRuntime(1261): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.newpicker/com.example.newpicker.MainActivity}: java.lang.IllegalStateException: 数据库/data/data/com.example.newpicker/databases/content.db (conn# 0) 已经关闭

从日志中我们看到,database already closed所以你必须做的是:

打开 db 并确保在运行游标代码之前它不会关闭:

    public Cursor getsomething()
    {
        // this.open(); // <-- explained below
        Cursor c= database.query(DatabaseHandler.Table_Name,magicdata,null, null, null,null,null); 

        // this.close(); // <-- if `this` refers to DB, it's a mistake, close DB at the end.
        return c;
    }

因此,在您的活动代码中,为了不让您的数据库打开,请使用以下内容:

X = new YourDatabaseClassName();  // new instance of your database (class contains getsomething method)
X.open(); // open DB
get=X.getsomething();
if (get != null && get.getCount() > 0) // also add a null check
{
    if  (get.moveToFirst())
    {
        do {
            String uname = get.getString(get.getColumnIndex("content_id"));
            String unknown =url + uname;
            String ax= get.getString(get.getColumnIndex("content"));
            downloadimages(url,ax); 
        }
        while (get.moveToNext());
    }
    get.close(); // also close the cursor
}
X.close(); // you end with DB so close it! next time it needs to reopened

这里也是一个android数据库教程。

于 2013-03-21T08:15:42.497 回答