openOrCreateDatabase()函數時 出現Failed to open the database錯誤

在使用SQLiteDatabase.openOrCreateDatabase()函數時 出現Failed to open the database錯誤,致使數據庫打不開。

Filef=context.getDatabasePath("downlog.db");
db=SQLiteDatabase.openOrCreateDatabase(FullPath,null);


懷疑是不是因爲data/data/xx/databases文件夾不存在所以創建不了數據庫,

 

 

修改後的代碼

File f=context.getDatabasePath("downlog.db").getParentFile();
              if(f.exists()==false)f.mkdirs();//注意是mkdirs()有個s 這樣可以創建多重目錄。
              FullPath=f.getPath()+"/downlog.db";
db=SQLiteDatabase.openOrCreateDatabase(FullPath,null);


數據庫可以正確創建了,這個問題解決了。

 

不過總覺得有點蹊蹺,爲什麼openOrCreateDatabase(FullPath,null);不會自己創建文件夾呢……

函數不是在activity中使用的,是在service中調用DBHelper類,DBHelper的構造函數中調用的。不知道是不是這個原因。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章