我遇到了SQLite问题。我一直在开发我的连接设备,而不是模拟器。这一直很好。我已经在手机和平板电脑上多次构建和安装了应用程序。大约一周前,我对我的资产文件夹中的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");
}
}
}
答案 0 :(得分:0)
运行unzip -l <your_app.apk>
并确保包含数据库。如果不存在,请尝试进行干净的构建。
答案 1 :(得分:0)
首先从不硬编码路径,因为不同的OEM可以随时更改您的应用无法使用getDatabasePath()
答案 2 :(得分:0)
嗨请重新下线。在你的代码中
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
而不是
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
由于