2012-06-04 88 views
0
public class DBOpener { 

private static String DB_PATH = "/data/data/com.edu.tp.iit.mns/databases/"; // path 
                      // of 
                      // our 
                      // database 
private static String DB_NAME = "finals"; // Database name 
private final Context myContext; 
private SQLiteDatabase db; 

public DBOpener(Context context) { 
    myContext = context; 
} 

public void open() { 
    boolean dbExists = checkDatabase(); 
    if (!dbExists) { 
     try { 
      copyDatabase(); 
     } catch (IOException e) { 
      throw new RuntimeException("Something bad!!!"); 
     } 
    } 
    String path = DB_PATH + DB_NAME; 
    db = SQLiteDatabase.openDatabase(path, null, 
      SQLiteDatabase.OPEN_READWRITE); 
} 

private boolean checkDatabase() { 
    SQLiteDatabase checkDB = null; 
    try { 
     // Try opening the database 
     String path = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(path, null, 
       SQLiteDatabase.OPEN_READONLY); 
    } catch (SQLiteException e) { 
     throw new RuntimeException("Something bad111111!!!"); 
    } 
    if (checkDB != null) 
     checkDB.close(); // Close the DB; we don’t need it now 
    return checkDB != null; 
} 

我的錯誤日誌文件:錯誤在訪問SQLite數據庫

06-04 14:00:38.756: E/Database(306): sqlite3_open_v2("/data/data/com.edu.tp.iit.mns/databases/finals", &handle, 1, NULL) failed 
    06-04 14:00:38.836: E/AndroidRuntime(306): FATAL EXCEPTION: main 
    06-04 14:00:38.836: E/AndroidRuntime(306): java.lang.RuntimeException: Unable to resume activity {com.edu.tp.iit.mns/com.edu.tp.iit.mns.Lunch}: java.lang.RuntimeException: Something bad111111!!! 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.os.Handler.dispatchMessage(Handler.java:99) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.os.Looper.loop(Looper.java:123) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.app.ActivityThread.main(ActivityThread.java:4627) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at java.lang.reflect.Method.invokeNative(Native Method) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at java.lang.reflect.Method.invoke(Method.java:521) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at dalvik.system.NativeStart.main(Native Method) 
    06-04 14:00:38.836: E/AndroidRuntime(306): Caused by: java.lang.RuntimeException: Something bad111111!!! 
    06-04 14:00:38.836: E/AndroidRuntime(306): at com.edu.tp.iit.mns.DBOpener.checkDatabase(DBOpener.java:50) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at com.edu.tp.iit.mns.DBOpener.open(DBOpener.java:29) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at com.edu.tp.iit.mns.Lunch.onResume(Lunch.java:49) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.app.Activity.performResume(Activity.java:3823) 
    06-04 14:00:38.836: E/AndroidRuntime(306): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118) 
    06-04 14:00:38.836: E/AndroidRuntime(306): ... 12 more 

我的程序運行良好,直到我刪除了資產的文件夾和數據/數據/數據庫文件夾中的數據庫,並放回到我的新的數據庫文件。由於這個錯誤,我無法繼續我的代碼。任何幫助?

+0

post your copyDatabase();代碼在這裏。 –

回答

3

修改方法checkDatabase()這樣的:

private boolean checkDatabase() { 
    File dbFile = new File(DB_PATH + DB_NAME); 
    return dbFile.exists(); 
} 

參考參見this link

+0

也適合我 – Mahesh