2011-02-18 44 views
2

我必須檢查數據庫是否存在或不在android中?我如何檢查?它應該在檢查後返回一個布爾值?plz給我提供代碼。在android中檢查數據庫

回答

1

喜歡的東西:

private boolean databaseExists() { 
    SQLiteDatabase checkDB = null; 
    try { 
     String myPath = DB_PATH + DATABASE_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null, 
      SQLiteDatabase.OPEN_READONLY); 
     if(checkDB != null){ 
      checkDB.close(); 
      return true; 
     } else { 
      //database does not exist 
      return false; 
     } 
    } catch (SQLiteException e) { 
     //database does not exist 
     return false; 
    } 
} 
+1

在這裏,`DB_PATH`應該是這樣的:` 「/數據/數據/ YOUR_PACKAGE_NAME_IN_MANIFEST /數據庫/」`。 – 2011-02-18 11:24:25