无法打开数据库openOrCreateDatabase

时间:2016-11-13 15:09:33

标签: android sqlite

我有以下代码:

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的权限,但它不起作用......

1 个答案:

答案 0 :(得分:0)

我应该使用/data/data/package_name/makac.db,所以我的情况/data/data/makac.main.com/makac.db