0

我有一个不适用于我的应用程序的现有 Sqlite 数据库。谁能知道如何帮助我?此外,我只用一张表制作了一个小测试 .db 文件,它工作正常。认为我发布图片的大数据库有问题,但我不确定是什么。

我已经发布了数据库外观的图片,如果您需要更多屏幕截图等,请询问。

http://tinypic.com/r/35arg38/6

这是 onCreate:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView contentLog = (TextView) findViewById(R.id.content_log);

        // Create the database
        DataBaseHelper myDbHelper = new DataBaseHelper(
                this.getApplicationContext());
        myDbHelper = new DataBaseHelper(this);

        try {
            myDbHelper.createDataBase();
            contentLog.append("Database Created\n");
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        // Open the database
        try {

            myDbHelper.openDataBase();
            contentLog.append("Database Opened\n");
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }

        // Get the readable version
        SQLiteDatabase db = myDbHelper.getReadableDatabase();
        contentLog.append("Get the readable database\n");

        // Make a select
        Cursor cur = db.rawQuery(
                "SELECT libelle FROM detail_verbe ORDER BY libelle ASC;", null);

        cur.moveToPosition(0);
        Log.v(TAG, "Nb Col:" + cur.getColumnCount());
        Log.v(TAG, "Nb Records:" + cur.getCount());
        cur.close();
        contentLog.append("Select:\t" + cur.getColumnCount() + " cols, "
                + cur.getCount() + " rows\n");

        // Make an insert
        ContentValues values = new ContentValues();
        values.put("libelle", "Serval");
        values.put("single_present", "Cat");
        long servalCatID = db.insert("detail_verbe", null, values);
        Log.v(TAG, "Serval Cat Inserted @: " + servalCatID);
        contentLog.append("Insert @ \t" + servalCatID + "\n");

        // Check insert
        cur = db.rawQuery(
                "SELECT libelle FROM detail_verbe ORDER BY libelle ASC;", null);

        cur.moveToPosition(0);
        Log.v(TAG, "Nb Col:" + cur.getColumnCount());
        Log.v(TAG, "Nb Records:" + cur.getCount());
        cur.close();
        contentLog.append("Select:\t" + cur.getColumnCount() + " cols, "
                + cur.getCount() + " rows\n");

        // dumb
        cur = db.rawQuery(
                "SELECT libelle FROM detail_verbe ORDER BY libelle ASC;",
                null);
        contentLog.append("\nDUMP\n");
        int i = 0;
        cur.moveToFirst();
        while (cur.isAfterLast() == false) {
            contentLog.append("(" + i++ + ")\t\t" + cur.getString(0) + "\t"
                    + cur.getString(1) + "\n");
            cur.moveToNext();
        }

        cur.moveToPosition(0);

        // Close
        myDbHelper.close();
        contentLog.append("Database closed.");

        // YEAH
    }

这是堆栈跟踪

    02-19 15:19:07.265: V/ExtDB(8663): Nb Col:1 
02-19 15:19:07.269: V/ExtDB(8663): Nb Records:13 
02-19 15:19:07.308: V/ExtDB(8663): Serval Cat Inserted @: 14 
02-19 15:19:07.308: V/ExtDB(8663): Nb Col:1 
02-19 15:19:07.312: V/ExtDB(8663): Nb Records:14 
02-19 15:19:07.312: E/CursorWindow(8663): Failed to read row 0, column 1 from a CursorWindow which has 14 rows, 1 columns. 
02-19 15:19:07.320: D/AndroidRuntime(8663): Shutting down VM 02-19 15:19:07.320: W/dalvikvm(8663): threadid=1: thread exiting with uncaught exception (group=0x41856300) 
02-19 15:19:07.363: E/AndroidRuntime(8663): FATAL EXCEPTION: main 
02-19 15:19:07.363: E/AndroidRuntime(8663): java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.stackr.android.externaldb/fr.stackr.android.externaldb.ExternalDBActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.app.ActivityThread.access$600(ActivityThread.java:130) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.os.Looper.loop(Looper.java:137) 02-19 15:19:07.363: E/AndroidRuntime(8663): at android.app.ActivityThread.main(ActivityThread.java:4745) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at java.lang.reflect.Method.invokeNative(Native Method) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at java.lang.reflect.Method.invoke(Method.java:511) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at dalvik.system.NativeStart.main(Native Method) 02-19 15:19:07.363: E/AndroidRuntime(8663): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.database.CursorWindow.nativeGetString(Native Method)
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.database.CursorWindow.getString(CursorWindow.java:434) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at fr.stackr.android.externaldb.ExternalDBActivity.onCreate(ExternalDBActivity.java:88) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.app.Activity.performCreate(Activity.java:5008) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
02-19 15:19:07.363: E/AndroidRuntime(8663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-19 15:19:07.363: E/AndroidRuntime(8663): ... 11 more
4

1 回答 1

0

你这里有问题:

cur.moveToPosition(0);
    Log.v(TAG, "Nb Col:" + cur.getColumnCount());
    Log.v(TAG, "Nb Records:" + cur.getCount());
    cur.close(); //cursor is closed
    //After closing, you call cur.get..() fix that
    contentLog.append("Select:\t" + cur.getColumnCount() + " cols, "
            + cur.getCount() + " rows\n");

如果您将重新查询(之后检查插入),则无需在此处调用 close() ,直到您知道您已完成游标,也许在关闭数据库之后将是一个不错的选择。

于 2013-02-19T15:40:51.523 回答