我想創建一個按鈕,根據數據庫是否存在切換到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