Java Android Studio SQLite无法打开数据库错误

Java Android Studio SQLite无法打开数据库错误,java,database,sqlite,android-sqlite,Java,Database,Sqlite,Android Sqlite,我已经检查了所有的解决方案,但我不断发现错误 数据库存在,我以前可以用相同的代码使用相同的数据库,但现在我遇到了这个错误。我需要那些以前收到过相同错误的人的帮助 谢谢 错误图像= 我的DbHelper类 public DbHelper(@Nullable Context context) { super(context, DB_NAME, null, 1); assert context != null; DB_PATH = context.getApplicatio

我已经检查了所有的解决方案,但我不断发现错误

数据库存在,我以前可以用相同的代码使用相同的数据库,但现在我遇到了这个错误。我需要那些以前收到过相同错误的人的帮助

谢谢

错误图像=

我的DbHelper类

public DbHelper(@Nullable Context context) {

    super(context, DB_NAME, null, 1);

    assert context != null;
    DB_PATH = context.getApplicationInfo().dataDir + "/databases/";

    openDataBase(); 
    this.mContext = context;

}


public void openDataBase() {

    String myPath = DB_PATH + DB_NAME;
    mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

}

public void copyDataBase() throws IOException {

    try {
        InputStream myInput = mContext.getAssets().open(DB_NAME);
        String outputFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outputFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0)
            myOutput.write(buffer, 0, length);

        myOutput.flush();
        myOutput.close();
        myInput.close();
        ;
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public boolean checkDataBase() {

   SQLiteDatabase tempDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        File file = new File(myPath);
        if(file.exists() && !file.isDirectory())
        tempDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

    } catch (SQLException e) {
        e.printStackTrace();
    }

    if (tempDB != null)
        tempDB.close();

    return tempDB != null ? true : false;
}

public void createDataBase() throws IOException {

    boolean isDBExist = checkDataBase();
    if (isDBExist) {

    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
我的主要活动

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    seekBar = (SeekBar)findViewById(R.id.seekBar);
    txtMode= (TextView) findViewById(R.id.txtMode);
    btnPlay = (Button)findViewById(R.id.btnPlay);
    btnScore = (Button)findViewById(R.id.btnScore);

    db = new DbHelper(this);
    try{
        db.createDataBase();
    }
    catch (IOException e){
        e.printStackTrace();
    }

当您在
db=newdbhelper中实例化DbHelper时(this)构造函数在数据库不存在时尝试打开数据库

public DbHelper(@Nullable Context context) {

    super(context, DB_NAME, null, 1);

    assert context != null;
    DB_PATH = context.getApplicationInfo().dataDir + "/databases/";

    openDataBase(); //<<<<<<<<<< WILL ONLY WORK IF DB EXISTS
    this.mContext = context;

}
在这种情况下,主要活动可能是:-

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    seekBar = (SeekBar)findViewById(R.id.seekBar);
    txtMode= (TextView) findViewById(R.id.txtMode);
    btnPlay = (Button)findViewById(R.id.btnPlay);
    btnScore = (Button)findViewById(R.id.btnScore);

    db = new DbHelper(this);
}

当您在
db=newdbhelper中实例化DbHelper时(this)构造函数在数据库不存在时尝试打开数据库

public DbHelper(@Nullable Context context) {

    super(context, DB_NAME, null, 1);

    assert context != null;
    DB_PATH = context.getApplicationInfo().dataDir + "/databases/";

    openDataBase(); //<<<<<<<<<< WILL ONLY WORK IF DB EXISTS
    this.mContext = context;

}
在这种情况下,主要活动可能是:-

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    seekBar = (SeekBar)findViewById(R.id.seekBar);
    txtMode= (TextView) findViewById(R.id.txtMode);
    btnPlay = (Button)findViewById(R.id.btnPlay);
    btnScore = (Button)findViewById(R.id.btnScore);

    db = new DbHelper(this);
}

这回答了你的问题吗@马图:不幸的是,它没有,我已经尝试过,并且不断得到相同的错误。这是否回答了你的问题@马图:不幸的是,它没有,我已经尝试过,并且不断地得到相同的错误是的,它成功了,非常感谢你,我的朋友。但是我有数据库,所以数据库是存在的,为什么我会出错。@如果数据库在从资产中复制之前不存在,那么在复制之前您试图打开它。是的,我明白了,您是对的。再次感谢您如果我运行API 27、28或29,我会再次遇到相同的错误:(@Efe这可能是因为您正在使用
this.getReadableDatabase();
。它创建了一个数据库并发布了Android 9。默认的数据库模式从Journal更改为WAL。看到了吗?是的,非常感谢你,我的朋友。但是我有db,所以db是存在的,为什么我会出错。@如果数据库在从资产复制之前不存在,那么在复制之前你就试图打开它。是的我明白了,你是对的。再次感谢你如果我运行API 27、28或29,我会再次遇到相同的错误:(@Efe这可能是因为你使用的是
this.getReadableDatabase();
。它创建了一个数据库,并在Android 9发布后,默认的数据库模式从Journal更改为WAL。请参阅