Android:无法打开Sqlite数据库

时间:2016-10-09 18:20:48

标签: android sqlite

我做了整整一个上午的研究。在Helper的构造函数中添加OpenOrCreateDatabase并不起作用,在清单中添加权限也没有。我也尝试重新安装该应用程序。 这是我的助手课程:

public class UserGameData extends SQLiteOpenHelper {
    SQLiteDatabase db;
    public static final String DB_NAME ="userdata.db";
    public static final int DB_VER = 1;


    public UserGameData(Context context) {
        super(context,DB_NAME, null,DB_VER);
        int i =0; // the error is this line for some reason??
        context.openOrCreateDatabase(DB_NAME,DB_VER,null);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String create = ("CREATE TABLE IF NOT EXISTS GameData (Character TEXT NOT NULL,Unlocked BOOLEAN NOT NULL DEFAULT 0, IntroVn BOOLEAN NOT NULL DEFAULT false, EndVn BOOLEAN NOT NULL DEFAULT false,HighScore int not null DEFAULT 0,Unlocks text not null DEFAULT yellow,UnlockCondition INTEGER NOT NULL DEFAULT 50, PRIMARY KEY (Character))");
        db.execSQL(create);
        init();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }

这是使用帮助程序的活动

public class Select extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select);
        UserGameData db= new UserGameData(this); //error occurred here
    }
    }

错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.reimu.buttonchange, PID: 21383
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.reimu.buttonchange/com.example.reimu.buttonchange.Select}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
                      at android.app.ActivityThread.access$900(ActivityThread.java:154)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:224)
                      at android.app.ActivityThread.main(ActivityThread.java:5526)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: 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:213)
                      at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:197)
                      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:806)
                      at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
                      at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
                      at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)
                      at com.example.reimu.buttonchange.UserGameData.<init>(UserGameData.java:23)
                      at com.example.reimu.buttonchange.Select.onCreate(Select.java:37)
                      at android.app.Activity.performCreate(Activity.java:6285)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
                      at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:224) 
                      at android.app.ActivityThread.main(ActivityThread.java:5526) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我也试过硬编码路径

public static final String DB_NAME ="/data/data/com.example.reimu.buttonchange/databases/userdata.db3userdata.db";

1 个答案:

答案 0 :(得分:1)

context.openOrCreateDatabase(DB_NAME,DB_VER,null)中的SQLiteOpenHelper不正确,不需要。如堆栈跟踪中所示,错误发生在那里。可能是因为第二个参数是1,这意味着MODE_WORLD_READABLEopenOrCreateDatabase()

删除该电话。

要使用SQLiteOpenHelper创建数据库文件,请在帮助程序上调用getWritableDatabase()getReadableDatabase()