我已经检查了所有解决方案,但我不断收到错误。
数据库存在,我可以使用相同的数据库之前使用相同的代码,但现在我收到此错误。我需要以前收到过相同错误的人的帮助。
谢谢你..
错误图像 = 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/