0

我有一段代碼訪問SQLite數據庫,直到果凍豆完美工作。在這種情況下,並非所有的數據都從數據庫中獲取(至少第一列被省略) 並且還給出了錯誤「未知錯誤(代碼14)無法打開數據庫」,儘管正在從中讀取一些數據。 如果您不瞭解情況,請詢問更多詳情Android Jelly Bean問題與SQLite連接

謝謝!

private class DataBaseHelper extends SQLiteOpenHelper { 

    private SQLiteDatabase myDataBase; 

    public DataBaseHelper() { 

     super(myContext, DB_NAME, null, 1); 

     DB_PATH = "/data/data/" + myContext.getPackageName() 
       + "/databases/"; 
    } 

    /** 
    * Creates a empty database on the system and rewrites it with your own 
    * database. 
    * */ 
    public void createDataBase() throws IOException { 
     boolean dbExist = checkDataBase(); 
     if (dbExist) { 

      // do nothing - database already exist 
     } else { 
      // By calling this method and empty database will be created 
      // into the default system path 
      // of your application so we are gonna be able to overwrite that 
      // database with our database. 
      this.getReadableDatabase(); 
      try { 
       copyDataBase(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 


    private boolean checkDataBase() { 

     SQLiteDatabase checkDB = null; 

     try { 
      String myPath = myContext.getDatabasePath(DB_NAME).getAbsolutePath(); 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.NO_LOCALIZED_COLLATORS); 

     } catch (SQLiteException e) { 
      // database does't exist yet. 
     } 

     if (checkDB != null) { 
      checkDB.close(); 
     } 

     return checkDB != null ? true : false; 
    } 

    /** 
    * Copies your database from your local assets-folder to the just 
    * created empty database in the system folder, from where it can be 
    * accessed and handled. This is done by transfering bytestream. 
    * */ 
    private void copyDataBase() throws IOException { 

     // Open your local db as the input stream 
     InputStream myInput = myContext.getAssets().open(DB_NAME); 

     // Path to the just created empty db 
     String outFileName = DB_PATH + DB_NAME; 

     // Open the empty db as the output stream 
     OutputStream myOutput = new FileOutputStream(outFileName); 

     // transfer bytes from the inputfile to the outputfile 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer)) > 0) { 
      myOutput.write(buffer, 0, length); 
     } 

     // Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 

    } 

    public void openDataBase() throws SQLException { 

     // Open the database 
     String myPath =DB_PATH + DB_NAME; 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.NO_LOCALIZED_COLLATORS); 

    } 

這裏是logcat的錯誤

 01-11 14:36:32.153: E/SQLiteLog(15050): (14) cannot open file at line 30174 of [00bb9c9ce4] 
    01-11 14:36:32.153: E/SQLiteLog(15050): (14) os_unix.c:30174: (2)  open(/data/data/eu.isdc.cabinCrew/databases/database.sqlite) - 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): Failed to open database '/data/data/eu.isdc.cabinCrew/databases/database.sqlite'. 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at eu.isdc.cabinCrew.a.b.c(Unknown Source) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at eu.isdc.cabinCrew.a.b.a(Unknown Source) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at eu.isdc.cabinCrew.a.a.<init>(Unknown Source) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at eu.isdc.cabinCrew.a.a.a(Unknown  Source) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at eu.isdc.cabinCrew.activities.TrainingActivity.onCreate(Unknown Source) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.app.Activity.performCreate(Activity.java:5008) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.app.ActivityThread.access$600(ActivityThread.java:130) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.os.Handler.dispatchMessage(Handler.java:99) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.os.Looper.loop(Looper.java:137) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at android.app.ActivityThread.main(ActivityThread.java:4745) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at java.lang.reflect.Method.invokeNative(Native Method) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at java.lang.reflect.Method.invoke(Method.java:511) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    01-11 14:36:32.183: E/SQLiteDatabase(15050): at dalvik.system.NativeStart.main(Native Method) 
    01-11 14:36:32.503: E/SQLiteLog(15050): (1) no such table: airdata 

它確實打開數據庫,但沒有發現有特定的表,即使它的存在。這隻發生在果凍豆。與這個版本的數據庫交互有什麼變化?

+0

那麼有什麼不工作,是應用程序崩潰?如果是這樣,發佈崩潰的追蹤 – hardartcore

+0

Ia增加了一些更多的細節 –

回答

0

這個問題是一種固定的,它仍然存在,但我發現真正的問題是躺在用自動適應的TextView適配器顯示數據在UI上,這不適用於果凍豆。 所以我仍然得到'無法打開數據庫'異常,但它仍然有效,數據庫被讀取。

0

您不能認爲數據庫將始終在「/ data/data /」處可用。相反,您需要使用getPath()來獲取數據庫文件的路徑。

+0

是的,我想是的,但這不是問題,我試着用'myContext.getDatabasePath(DB_NAME).getAbsolutePath();'並沒有改變。事情是,數據庫被讀取,但部分,所以路徑是好的 –