我想创建一个按钮,根据数据库是否存在切换到2个活动之一。我做了一个databasecheckhelper,但由于某种原因,即使数据库存在,它仍然会发出错误信息。Android为什么我的checkDB一直给出错误?
代码上点击按钮:
public void open_my_training(View view) {
Intent intent;
boolean databaseExists = checkDatabase.checkDB(this);
if(databaseExists){
intent = new Intent(this, a.class);
}else{
intent = new Intent(this, b.class);
}
startActivity(intent);
}
助手
public class checkDatabase {
public static boolean checkDB(Context context) {
File dbFile = context.getDatabasePath("database.db");
return dbFile.exists();
}
}
谁能告诉我什么,我做错了什么?
编辑:
因为代码似乎是罚款我加入我的代码创建数据库:
public void save_training(View view) {
CheckBox box1 = (CheckBox) findViewById(R.id.box1);
CheckBox box2 = (CheckBox) findViewById(R.id.box2);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
String spinner1 = spinner1.getSelectedItem().toString();
createDatabase();
addTraining(box1.isChecked(), box2.isChecked(), spinner1);
}
private void createDatabase() {
try {
trainingDB = this.openOrCreateDatabase("database.sqlite", MODE_PRIVATE, null);
trainingDB.execSQL("CREATE TABLE IF NOT EXISTS table1" + "(id integer primary key," +
"box1 boolean, box2 boolean, + "spinner1 VARCHAR);");
}
private void addTraining(boolean box1Checked, boolean box2Checked, String spinner1) {
trainingDB.execSQL("INSERT INTO table1 (box1, box2, spinner1) VALUES ('"+ box1Checked + "', '" +
box2Checked + "', '" + spinner1 + "');");
}
确保你的db文件是'database.db'或'database.sqlite'? –
似乎并没有改变任何东西 – Mischa
RU能够使用创建数据库befor使用? – Sush