2016-06-21 41 views
0

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

確保你的db文件是'database.db'或'database.sqlite'? –

+0

似乎並沒有改變任何東西 – Mischa

+0

RU能夠使用創建數據庫befor使用? – Sush

回答

1

更換

File dbFile = context.getDatabasePath("database.sqlite");

,而不是

File dbFile = context.getDatabasePath("database.db");

+0

我試過這個,但它似乎沒有改變任何東西。 – Mischa

+0

你嘗試使用模擬器或設備? –

+0

我使用設備來運行應用程序。在使用調試器時,我仍然是一個noob,所以我不知道問題出在哪裏。 – Mischa