0

我在android應用程序中使用外部數據庫,它在所有模擬器和三星真實設備上工作正常。但是,當我檢查宏基智能手機時,我的應用程序崩潰,出現以下異常:在某些設備上的android「沒有這樣的表」問題

android.database.sqlite.SQLiteException: no such table: 

這對我很奇怪。我在文件資源管理器中的數據文件夾下檢查了我的數據庫和所有表格。

我不明白爲什麼會發生這種情況。

請指導我。由於在SQL輔助類的

代碼提前

是如下:

public class MyDatabaseHelper extends SQLiteOpenHelper { 

    // System path of application database. 
    private static String DB_PATH = MyApplication.getAppContext() 
      .getFilesDir().getParentFile().getPath() 
      + "/databases/"; 
    private static String DB_NAME = "myDB"; 
    private SQLiteDatabase myDataBase; 
    private final Context myContext; 

    /** 
    * Constructor Takes and keeps a reference of the passed context in order to 
    * access to the application assets and resources. 
    * 
    * @param context 
    */ 
    public MyDatabaseHelper(Context context) { 
     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
    } 

    /** 
    * Creates a empty database on the system and rewrites it with own database. 
    * 
    */ 
    public void createDataBase() throws IOException { 

     checkDataBase(); 
     // Creates empty database default system path 
     this.getReadableDatabase(); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      throw new Error("Error copying database"); 
     } 
    } 

    /** 
    * Checks if the database already exist to avoid re-copying the file each 
    * time whenever the application opened. 
    * 
    * @return true if it exists, false if it doesn't 
    */ 
    private boolean checkDataBase() { 

     SQLiteDatabase checkDB = null; 
     try { 
      String myPath = DB_PATH + DB_NAME; 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READONLY); 
     } catch (SQLiteException e) { 
      e.printStackTrace(); 
     } 

     if (checkDB != null) { 
      checkDB.close(); 
     } 
     return checkDB != null ? true : false; 
    } 

    /** 
    * Copies database from local assets-folder to the just created empty 
    * database in the system folder and from where it can be accessed and 
    * handled using byte stream transferring. 
    * 
    */ 
    private void copyDataBase() throws IOException { 

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

     // Path of 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 input file to the output file 
     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 SQLiteDatabase openDataBase() throws SQLException { 

     // Opens the database 
     String myPath = DB_PATH + DB_NAME; 
     return SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READONLY); 
    } 

    @Override 
    public synchronized void close() { 
     if (myDataBase != null) 
      myDataBase.close(); 
     super.close(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
     // TODO Auto-generated method stub 
    } 
} 
+0

在這裏添加java代碼助手類。 – Sajmon 2013-03-18 10:51:28

+0

你使用默認的數據庫路徑嗎? – Abx 2013-03-18 10:52:02

回答

1

最後我解決了我的問題。

我剛剛將可讀的數據庫導入數據庫,並在打開它後關閉它。我完整的代碼如下:

public class MyDatabaseHelper extends SQLiteOpenHelper { 

    // System path of application database. 
    private static String DB_PATH = MyApplication.getAppContext() 
      .getFilesDir().getParentFile().getPath() 
      + "/databases/"; 
    private static String DB_NAME = "myDB"; 
    private SQLiteDatabase myDataBase; 
    private final Context myContext; 

    /** 
    * Constructor Takes and keeps a reference of the passed context in order to 
    * access to the application assets and resources. 
    * 
    * @param context 
    */ 
    public MyDatabaseHelper(Context context) { 
     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
    } 

    /** 
    * Creates a empty database on the system and rewrites it with own database. 
    * 
    */ 
    public void createDataBase() throws IOException { 

     checkDataBase(); 
     SQLiteDatabase db_Read = null; 

     // Creates empty database default system path 
     db_Read = this.getReadableDatabase(); 
     db_Read.close(); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      throw new Error("Error copying database"); 
     } 
    } 

    /** 
    * Checks if the database already exist to avoid re-copying the file each 
    * time whenever the application opened. 
    * 
    * @return true if it exists, false if it doesn't 
    */ 
    private boolean checkDataBase() { 

     SQLiteDatabase checkDB = null; 
     try { 
      String myPath = DB_PATH + DB_NAME; 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READONLY); 
     } catch (SQLiteException e) { 
      e.printStackTrace(); 
     } 

     if (checkDB != null) { 
      checkDB.close(); 
     } 
     return checkDB != null ? true : false; 
    } 

    /** 
    * Copies database from local assets-folder to the just created empty 
    * database in the system folder and from where it can be accessed and 
    * handled using byte stream transferring. 
    * 
    */ 
    private void copyDataBase() throws IOException { 

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

     // Path of 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 input file to the output file 
     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 SQLiteDatabase openDataBase() throws SQLException { 

     // Opens the database 
     String myPath = DB_PATH + DB_NAME; 
     return SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READONLY); 
    } 

    @Override 
    public synchronized void close() { 
     if (myDataBase != null) 
      myDataBase.close(); 
     super.close(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
     // TODO Auto-generated method stub 
    } 
} 

我發現我的答案here

0

也許你已硬編碼的數據庫路徑。對於不同的設備可能會有所不同。使用Envirenment.getExternalStorageDirectory()創建數據庫的路徑。

0

嘗試用這種私人靜態字符串DB_PATH =「/數據/數據/你的包名/數據庫/」;