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

标签 java database sqlite android-sqlite

我已经检查了所有解决方案,但我不断收到错误。

数据库存在,我可以使用相同的数据库之前使用相同的代码,但现在我收到此错误。我需要以前收到过相同错误的人的帮助。

谢谢你..

错误图像 = https://ibb.co/GxBFznf

我的 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();
        }
    }
}

我的主要 Activity ;

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 = new DbHelper(this); 那样实例化 DbHelper 时,构造函数会在数据库不存在时尝试打开数据库。

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;

}

您可以使用:-

public DbHelper(@Nullable Context context) {

    super(context, DB_NAME, null, 1);
    assert context != null;
    this.mContext = context;
    DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
    if (!checkDataBase()) {
        try {
            createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Unable to Copy Database");
        }
    }
    openDataBase();        
}

在这种情况下,MainActivity 可能是:-

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);
}

关于java - Android Studio SQLite 无法打开数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59252284/

相关文章:

java - 如何在 onClick() 中禁用/更改 wicket 按钮链接的样式

node.js - 使用 Express 和 MongoDB 从多个单独的集合中获取数据

android - 如何迭代具有多个结果的连接表?

android - Android 中使用多个数据库表时的代码设计

java - 如何使用 apache-commons-io FileUtils.listFilesAndDirs 过滤某个目录的所有目录和子目录?

java - Maven 从环境变量构建 : set settings. xml 路径

mysql - 多登录区

c# - 短小精悍: "Insufficient parameters supplied to the command"

java - 如何使用正则表达式验证字符串必须以字母开头,所有其他字符在java中可以是字母/数字/空格

php - 如何从字段表返回数据 api(fractal) 已设置默认值?