java - 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();
}
解决方案
当您在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);
}
推荐阅读
- python - 从python执行时grep无法找到字符串
- mysql - 如何通过adodb连接datagrid和mysql数据库
- python - model.fit_generator : 'tuple' 对象不能被解释为整数
- javascript - 当用户关闭 Android/IOS Cordova/Capacitor 应用程序时,是否可以检测并执行操作?
- reactjs - 反应docker nginx kubernetes 404
- tcl - 如何将目录中的第一个文件设置为 TCL 中的变量?
- c++11 - C++ 声明和初始化/变量未初始化为默认值
- python - Python:如果满足/不满足条件,需要停止运行代码
- dart - 我怎样才能要求一个类在 Dart 中有 fromJson?
- php - 'PHP 消息:PHP 警告:未定义的数组键“lastpage”问题