這是我的第一個應用程序Android
。我在這件事上看到了一些問題,但沒有人爲我解決問題。無法打開資產文件夾中的sqlite文件複製到設備
我的資產文件夾中的數據庫,fmdb.sqlite命名。我以前從這個鏈接的代碼示例: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
當我到了,應該讓流在資產文件夾(copyDataBase法)的源碼文件中的行,我得到一個異常「java.io.FileNotFoundException: fmdb.sqlite」。
誰能告訴我我做錯了什麼?這是我的班。
public class DatabaseOpener extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
//The Android's default system path of your application database.
public static String DB_PATH;
public static String DB_NAME = "fmdb.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DatabaseOpener(Context context) throws IOException {
super(context, DB_NAME, null, DATABASE_VERSION);
myContext = context;
}
/**
* Check if the database already exists to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH;
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;
}
/**
* 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) {
throw new Error("Error copying database");
}
}
}
/**
* 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;
//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 SQLiteDatabase openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH;// + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
return myDataBase;
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
編輯2 這裏是logcat的。我卸載並重新運行該程序。現在是關於打開數據庫文件寫作,我非常感謝你的幫助!
09-29 17:59:19.540: W/SQLiteAssetHelper(27063): copying database from assets... 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): Couldn't open fmdb.sqlite for writing (will try read-only): 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/fmdb.sqlite file (or .zip, .gz archive) in assets, or target folder not writable 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at android.content.res.AssetManager.openAsset(Native Method) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at android.content.res.AssetManager.open(AssetManager.java:315) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at android.content.res.AssetManager.open(AssetManager.java:289) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.copyDatabaseFromAssets(SQLiteAssetHelper.java:436) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.createOrOpenDatabase(SQLiteAssetHelper.java:400) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:176) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at com.friendlymeter.dal.MyDatabase.getSample(MyDatabase.java:27) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at com.friendlymeter.app.MainActivity$2.onClick(MainActivity.java:105) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at android.view.View.performClick(View.java:4204) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at android.view.View$PerformClick.run(View.java:17355) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at android.os.Handler.handleCallback(Handler.java:725) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at android.os.Handler.dispatchMessage(Handler.java:92) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at android.os.Looper.loop(Looper.java:137) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at android.app.ActivityThread.main(ActivityThread.java:5041) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at java.lang.reflect.Method.invokeNative(Native Method) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at java.lang.reflect.Method.invoke(Method.java:511) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 09-29 17:59:19.689: E/SQLiteAssetHelper(27063): at dalvik.system.NativeStart.main(Native Method) 09-29 17:59:19.709: E/SQLiteLog(27063): (14) cannot open file at line 30176 of [00bb9c9ce4] 09-29 17:59:19.709: E/SQLiteLog(27063): (14) os_unix.c:30176: (2) open(/data/data/com.friendlymeter.app/databases/fmdb.sqlite) - 09-29 17:59:19.909: D/dalvikvm(27063): GC_CONCURRENT freed 130K, 9% free 2652K/2904K, paused 9ms+15ms, total 171ms 09-29 17:59:20.019: E/SQLiteDatabase(27063): Failed to open database '/data/data/com.friendlymeter.app/databases/fmdb.sqlite'. 09-29 17:59:20.019: E/SQLiteDatabase(27063): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:264) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at com.friendlymeter.dal.MyDatabase.getSample(MyDatabase.java:27) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at com.friendlymeter.app.MainActivity$2.onClick(MainActivity.java:105) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.view.View.performClick(View.java:4204) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.view.View$PerformClick.run(View.java:17355) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.os.Handler.handleCallback(Handler.java:725) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.os.Handler.dispatchMessage(Handler.java:92) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.os.Looper.loop(Looper.java:137) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at android.app.ActivityThread.main(ActivityThread.java:5041) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at java.lang.reflect.Method.invokeNative(Native Method) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at java.lang.reflect.Method.invoke(Method.java:511) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 09-29 17:59:20.019: E/SQLiteDatabase(27063): at dalvik.system.NativeStart.main(Native Method)
請[使用'SQLiteAssetHelper'(https://github.com/jgilfelt/android-sqlite-asset-helper)用於包裝數據庫一項資產。 – CommonsWare 2014-09-23 21:00:26
DB_PATH始終爲空。這段代碼應該如何工作? – greenapps 2014-09-23 21:15:46
@greenapps類中有一個靜態部分,用於初始化DB_PATH(不包括在代碼中,但它在那裏) – amazingwolf 2014-09-25 13:43:49