我有一個SQLite問題。我一直在用我的連接設備進行開發,而不是仿真器。這一直很好。我已經在我的手機和平板電腦上多次構建並安裝了該應用程序。大約一週前,我對我的資產文件夾中的SQLite數據庫進行了更改。我從我的設備上卸載了應用程序,因爲我想要一個乾淨的安裝,但現在看起來數據庫永遠不會到達那裏。這是一直在那裏的代碼。這種事情:Android應用安裝不包括SQLite數據庫
請參閱新的代碼示例底部
private static String _DB_PATH = "/data/data/com.android.myapp/databases/";
private static final String _DATABASE_NAME = "myapp.sqlite";
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = _DB_PATH + _DATABASE_NAME;
***//THE PROBLEM IS THIS LINE, FOR WHATEVER REASON IT'S NO LONGER FINDING THE DATABASE IN THE ASSETS FOLDER
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;
}
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.
try {
getReadableDatabase();
copyDataBase();
}
catch (IOException e) {
throw new Error("Error copying database");
}
}
}
它安裝沒有錯誤,但是當我做一些事來訪問設備上的數據庫,它炸彈上getReadableDatabase();.
這裏的logcat的:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.myapp/com.android.myapp.ActivityL}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
at android.app.ActivityThread.access$1500(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4025)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
11-09 21:55:26.250: E/AndroidRuntime(11474): Caused by: java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:223)
at com.yesjapan.android.database.DatabaseHelper.createDataBase(DatabaseHelper.java:82)
at com.yesjapan.android.database.DbAdapter.open(DbAdapter.java:71)
at com.android.myapp.FragmentP.LoadC(FragmentP.java:226)
at com.android.myapp.FragmentP.onCreateView(FragmentP.java:97)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:735)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:926)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:909)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1584)
at android.app.Activity.performStart(Activity.java:4377)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1721)
我可以安裝一個以前的版本,它似乎工作。我已經從頭開始刪除並重建數據庫。我不知道爲什麼它不認爲那裏有數據庫,或者爲什麼數據庫沒有安裝。
這是非常令人沮喪的是花時間在這個廢話時,有代碼寫入! arrgh!
編輯: 這裏是另一個常見的錯誤
11-10 17:33:55.983: E/Database(22087): sqlite3_open_v2("/data/data/com.android.myapp/databases/myapp.sqlite", &handle, 2, NULL) failed
編輯:新的代碼示例
private boolean checkDataBase() {
//SQLiteDatabase checkDB = null;
File dbfile = null;
try {
//String myPath = _DB_PATH + _DATABASE_NAME;
//File myPath = _helperContext.getDatabasePath(_DATABASE_NAME);
//checkDB = SQLiteDatabase.openDatabase(myPath.toString(), null, SQLiteDatabase.OPEN_READWRITE);
String myPath = _DB_PATH + _DATABASE_NAME;
dbfile = new File(myPath);
//checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
Log.e("DatabaseHelper", e.getMessage());
}
//if (checkDB != null) checkDB.close();
return dbfile.exists();
//return checkDB != null;
}
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.
getReadableDatabase();
try {
copyDataBase();
}
catch (IOException e) {
throw new Error("Error copying database");
}
}
}
多大的數據庫文件? –
請顯示代碼'getReadableDatabase()'。而且,最好更改'return checkDB!= null? true:false;'返回checkDB!= null;'即使它不能解決問題。 – wannik
@wannik它內置 SQLiteDatabase android.database.sqlite.SQLiteOpenHelper.getReadableDatabase() 公共同步SQLiteDatabase getReadableDatabase() 自:API等級1 – Metallicraft