我创建了一个名为“CookbookDB”的数据库并将其放入我的资源文件夹中。 注意:我将表格的主要ID字段更改为_id。
我的代码出错了。有什么帮助吗?
错误:
11-08 21:20:25.074: D/dalvikvm(624): Not late-enabling CheckJNI (already on)
11-08 21:20:25.534: E/Trace(624): error opening trace file: No such file or directory (2)
11-08 21:20:25.734: E/SQLiteLog(624): (14) cannot open file at line 30174 of [00bb9c9ce4]
11-08 21:20:25.734: E/SQLiteLog(624): (14) os_unix.c:30174: (2) open(/data/data/com.cookbook.adiel/databases/CookbookDB) -
11-08 21:20:25.755: E/SQLiteDatabase(624): Failed to open database '/data/data/com.cookbook.adiel/databases/CookbookDB'.
11-08 21:20:25.755: E/SQLiteDatabase(624): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
11-08 21:20:25.755: E/SQLiteDatabase(624): at com.cookbook.adiel.ExistingDBHandler.checkDataBase(ExistingDBHandler.java:61)
11-08 21:20:25.755: E/SQLiteDatabase(624): at com.cookbook.adiel.ExistingDBHandler.createDataBase(ExistingDBHandler.java:42)
11-08 21:20:25.755: E/SQLiteDatabase(624): at com.cookbook.adiel.MainActivity.onCreate(MainActivity.java:43)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.app.Activity.performCreate(Activity.java:5008)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.os.Looper.loop(Looper.java:137)
11-08 21:20:25.755: E/SQLiteDatabase(624): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-08 21:20:25.755: E/SQLiteDatabase(624): at java.lang.reflect.Method.invokeNative(Native Method)
11-08 21:20:25.755: E/SQLiteDatabase(624): at java.lang.reflect.Method.invoke(Method.java:511)
11-08 21:20:25.755: E/SQLiteDatabase(624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-08 21:20:25.755: E/SQLiteDatabase(624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-08 21:20:25.755: E/SQLiteDatabase(624): at dalvik.system.NativeStart.main(Native Method)
11-08 21:20:25.755: D/AndroidRuntime(624): Shutting down VM
11-08 21:20:25.755: W/dalvikvm(624): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-08 21:20:25.765: E/AndroidRuntime(624): FATAL EXCEPTION: main
11-08 21:20:25.765: E/AndroidRuntime(624): java.lang.Error: Database doesn't exist yet
11-08 21:20:25.765: E/AndroidRuntime(624): at com.cookbook.adiel.ExistingDBHandler.checkDataBase(ExistingDBHandler.java:63)
11-08 21:20:25.765: E/AndroidRuntime(624): at com.cookbook.adiel.ExistingDBHandler.createDataBase(ExistingDBHandler.java:42)
11-08 21:20:25.765: E/AndroidRuntime(624): at com.cookbook.adiel.MainActivity.onCreate(MainActivity.java:43)
11-08 21:20:25.765: E/AndroidRuntime(624): at android.app.Activity.performCreate(Activity.java:5008)
11-08 21:20:25.765: E/AndroidRuntime(624): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-08 21:20:25.765: E/AndroidRuntime(624): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-08 21:20:25.765: E/AndroidRuntime(624): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-08 21:20:25.765: E/AndroidRuntime(624): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-08 21:20:25.765: E/AndroidRuntime(624): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-08 21:20:25.765: E/AndroidRuntime(624): at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 21:20:25.765: E/AndroidRuntime(624): at android.os.Looper.loop(Looper.java:137)
11-08 21:20:25.765: E/AndroidRuntime(624): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-08 21:20:25.765: E/AndroidRuntime(624): at java.lang.reflect.Method.invokeNative(Native Method)
11-08 21:20:25.765: E/AndroidRuntime(624): at java.lang.reflect.Method.invoke(Method.java:511)
11-08 21:20:25.765: E/AndroidRuntime(624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-08 21:20:25.765: E/AndroidRuntime(624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-08 21:20:25.765: E/AndroidRuntime(624): at dalvik.system.NativeStart.main(Native Method)
我的数据库处理程序类的复制方法部分:
private static String DB_PATH = "/data/data/com.cookbook.adiel/databases/";
private static String DB_NAME = "CookbookDB";
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing, database already exists
}
else{
this.getDatabaseName();
try{
copyDataBase();
}catch(IOException e){
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
throw new Error("Database doesn'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; // Same as String myPath in checkDataBase()
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from myInput to myOutput
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();
}
在启动器上:
ExistingDBHandler dbHandler = new ExistingDBHandler(this);
try {
dbHandler.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i =1; i<stories.length + 1;i++){
stories[i-1] = dbHandler.getStory(i);
}
List<StoryType> storyList = dbHandler.getAllStories();
for(int i = 0; i<stories.length; i++){
Log.d("Checking if db is working", "Story " + i);
}
任何帮助都会非常感激。谢谢!