2016-11-13 47 views
0

我有以下代碼:無法打開數據庫openOrCreateDatabase

public class UserDataReceiverService extends BroadcastReceiver { 

private static UserDataReceiverService instance; 

private SQLiteDatabase database; 

public UserDataReceiverService() { 
    instance = this; 
    database = SQLiteDatabase.openOrCreateDatabase("/data/data/makac.db", null); 
    database.execSQL("CREATE TABLE IF NOT EXISTS users(username VARCHAR, weight DECIMAL);"); 
} 
... 
} 

它的單例模式。我想要做的就是打開或創建SQLite數據庫,但我總是得到

android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 
                   at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 
                   at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) 
                   at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 
                   at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 
                   at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 
                   at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 
                   at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804) 
                   at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789) 
                   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 
                   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709) 
                   at com.main.makac.receivers.UserDataReceiverService.<init>(UserDataReceiverService.java:25) 
                   at com.main.makac.receivers.UserDataReceiverService.getInstance(UserDataReceiverService.java:50) 
                   at com.main.makac.UserActivity.onCreate(UserActivity.java:41) 
                   at android.app.Activity.performCreate(Activity.java:5231) 
                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)  
                   at android.app.ActivityThread.access$800(ActivityThread.java:135)  
                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  
                   at android.os.Handler.dispatchMessage(Handler.java:102)  
                   at android.os.Looper.loop(Looper.java:136)  
                   at android.app.ActivityThread.main(ActivityThread.java:5017)  
                   at java.lang.reflect.Method.invokeNative(Native Method)  
                   at java.lang.reflect.Method.invoke(Method.java:515)  

爲什麼它,如果它不存在,不創建數據庫?我要求它創建或打開,它只是試圖打開它,然後失敗。

我也使用權限WRITE_EXTERNAL_STORAGE但它只是不工作...

回答

0

我應該在我的情況/data/data/makac.main.com/makac.db

已經使用 /data/data/package_name/makac.db所以