2013-02-19 100 views
0

我有一個現有的Sqlite數據庫不能與我的應用程序一起工作。任何人都可以知道如何幫助我嗎?另外我用一個表做了一個小測試.db文件,它工作。認爲我張貼圖片的大數據庫有問題,但我不知道是什麼。Sqlite數據庫不適用於我的Android應用程序

我已經發布了什麼樣的數據庫看起來像,如果你需要screenchoots等問。

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 
+0

「但現有的不」 - 這是對你的症狀描述不充分。 「我發佈了數據庫看起來像的圖片」 - 在哪裏? – CommonsWare 2013-02-19 15:10:30

+0

圖片在鏈接中。和.db文件不會在我的應用程序中打開 – user2047427 2013-02-19 15:11:44

+0

更多信息或某些代碼會很好。 LogCat提供哪些錯誤消息?你如何嘗試創建數據庫? – Vion 2013-02-19 15:12:07

回答

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"); 

如果你將重新查詢(t o之後檢查插入),不需要在那裏調用close(),直到你知道你用光標完成了,也許在關閉db之後,這將是一個不錯的選擇。

+0

我將cur.close()移到myDBHelper.close()之後,但它仍然不起作用。它只是崩潰。 – user2047427 2013-02-19 15:47:29

+0

在你調用close()的代碼中有兩個不同的地方,你是否修復了這兩個地方? – 2013-02-19 15:50:17

+0

是的,我在這兩個地方。 – user2047427 2013-02-19 15:51:53

相關問題