首页 > 解决方案 > Android Studio 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();
        }
    }
}

我的主要活动;

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

标签: javadatabasesqliteandroid-sqlite

解决方案


当您在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);
}

推荐阅读