2011-07-21 69 views
0

我已經寫了創建數據庫的代碼,我得到ERROR/AndroidRuntime(7193):在com.android.xont.db.DataBaseHelper.createDataBase(DataBaseHelper.java:43)Android數據庫創建錯誤。

這是我的代碼:

public class DataBaseHelper extends SQLiteOpenHelper { 
    private static String DB_PATH = "/data/data/com.android.xont.controller/databases/"; 
    private static String DB_NAME = "DEMOUserVentura_HEMA.db"; 
    private SQLiteDatabase DEMOUserVentura_HEMA; 
    private final Context myContext; 


    private DataBaseHelper myDbHelper; 

public DataBaseHelper(Context context) { 
    super(context, DB_NAME, null, 1); 
    this.myContext = context; 
} 

/** 
* Creates a empty database on the system and rewrites it with your own 
* database. 
**/ 
public void createDataBase() throws IOException { 
    boolean dbExist = checkDataBase(); 
    if (dbExist) { 
    } else { 
     this.getReadableDatabase().close(); 
     //this.getReadableDatabase().getPath(); 
     //System.out.println(" ===== " + this.getReadableDatabase().getPath()); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      throw new Error(e); 
     } 
    } 

} 

private boolean checkDataBase() { 
    SQLiteDatabase checkDB = null; 
    try { 
     String myPath = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY); 
    } catch (SQLiteException e) { 
     // database does't exist yet. 
    } 

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

    return checkDB != null ? true : false; 
} 

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; 
    DEMOUserVentura_HEMA = SQLiteDatabase.openDatabase(myPath, null, 
      SQLiteDatabase.OPEN_READONLY); 

} 

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

@Override 
public void onCreate(SQLiteDatabase db) { 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
} 
} 

當我打電話給這個活動時,我得到一個異常。我的數據庫大小是:4.55MB。

當我打印getReadableDatabase()。getPath(),這是正確返回數據庫路徑。

請幫我一把。我一直在拉我的頭髮一個星期。

當我運行模擬器時,它會複製少量的文件。並非全部。然後我做了手動方式複製。

現在的問題是HTC手機。如何解決這個例外。請幫幫我。

enter image description here

在此先感謝..

回答

2

的AssetManager有文件大小限制。在某些設備上,大於1MB的資產文件無法打開。您必須將文件拆分爲更小的塊,將其放入資產文件夾中,並在應用運行時重新組合塊。

請參閱this question關於如何重新組裝塊。

要在OSX或Linux上創建塊,您可以使用命令行工具'split'。

herehere或使用谷歌關鍵字「機器人databasehelper資產」,以找到如何實現從當DB是雙頭呆了第一次的資源文件夾重新組裝塊一個DatabaseHelper教程。

+0

感謝您的鏈接。那是什麼createNewFile()方法?你有完整的源代碼嗎? – Piraba

+0

查看關於示例代碼的編輯 – thaussma

+0

如果我們使用android 2.3.3,那麼我們不需要做大塊? – Piraba