2012-09-27 151 views
1

我有一個SQLite數據庫(.db的),複製它的資產的文件夾,並有將其複製到/數據/數據/包名/數據庫。問題是我沒有數據庫文件夾,並沒有成功創建它。要創建「數據庫」文件夾中存儲SQLite數據庫

String dirPath = "/data/data/com.gatec.douaa/databases/"; 
Log.d("Directory",dirPath); 
File projDir = new File(dirPath); 
if (!projDir.exists()) 
     projDir.mkdirs(); 

你能幫我創建該文件夾來存儲數據庫嗎? 非常感謝。

+3

請考慮使用'SQLiteAssetHelper',它處理這一切爲您:https://github.com/jgilfelt/android-sqlite-asset-helper – CommonsWare

+0

@CommonsWare :所以不需要將數據庫複製到/ databases文件夾?優秀會嘗試:)。 – androniennn

+0

數據庫仍然需要在那裏複製,只是這樣做的重要舉措是爲你完成的。 – Barak

回答

0

在我experieces的「數據庫」目錄中創建您使用的數據庫的第一次。因此,如果要從「資產」目錄複製數據庫,則必須強制在「data/data/package_name/databases」路徑中創建數據庫。 這隻發生在棒棒糖(android 5),在我的經驗。 然後你可以使用下面的代碼:

File file = new File(context.getDatabasePath("database_name.db").getPath()); 
    if (!file.exists()) 
    { 
     try 
     { 
      file.createNewFile(); 
     } 
     catch (IOException e) 
     { 

     } 
    }