-2
我想將sqlite數據庫文件保存在外部存儲器中。在外部存儲器中保存sqlite數據庫
我編寫的代碼保存在如下的內部內存中。但我想在運行時提取「xxx.db」並檢查我的數據庫字段是否已保存。
的manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
和DBHelper.java
private static String DB_PATH = "data/data/com.example.myApp/databases/";
private static final String DATABASE_TABLE = "myDB";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydb.db";
public void createDatabase() throws IOException{
boolean doesDbExist = checkDatabase();
if(doesDbExist){
Toast.makeText(ourContext, "DB Already Created", Toast.LENGTH_SHORT).show();
}else{
this.getReadableDatabase();
try{
copyDatabase();
}catch(Exception e){
Log.d("DBError", "Copy DB error");
}
Toast.makeText(ourContext, "New DB created.", Toast.LENGTH_SHORT).show();
}
}
private void copyDatabase() throws IOException{
//Open your local db as the input stream
InputStream myInput = ourContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DATABASE_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 len;
while ((len = myInput.read(buffer))>0) {
myOutput.write(buffer, 0, len);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
請幫助我,並建議我需要改變。
不工作:(。有沒有其他的解決方案?> – bibox
爲什麼它不工作?究竟發生了什麼? – SirGregg