2015-10-16 113 views
1
public void onClick(View v) { 

      if (btn66 == v) { 
       ContentValues value = new ContentValues(); 
       value.put(DBhelper.Amount, txtBudget.getText().toString()); 
       value.put(DBhelper.Description, txr.getText().toString()); 

       if(DBhelper.Amount == null) 
       { 
        db = helper.getWritableDatabase(); 
        db.insert(DBhelper.TABLE2, null, value); 
        db.close(); 
        clearfield(); 
        Toast.makeText(this, "Budget add Successfully", Toast.LENGTH_LONG).show(); 
        fetchData2(); 
        Intent i = new Intent(addbudget.this, MainActivity.class); 
        startActivity(i); 

       } 
       else{ 
        db = helper.getWritableDatabase(); 
        db.update(DBhelper.TABLE2, value, "_id "+"="+1, null); 
        db.close(); 

        fetchData2(); 
        Toast.makeText(this, "Update Successfully", Toast.LENGTH_LONG).show(); 
        clearfield(); 
       } 
      } 
     } 

以上是我的代碼添加和更新值的sqlite數據庫後,我的代碼中包含更新方法,我的add函數不工作和我的更新功能也無法正常工作。是否有任何問題與我的代碼,但我沒有得到任何錯誤消息。爲什麼我不能更新sqlite數據庫的值

這是我的數據庫表

 static final String C_ID = "_id"; 
     static final String Name = "name"; 
     static final String B_ID = "_id"; 

     public void onCreate(SQLiteDatabase db){ 

       db.execSQL("CREATE TABLE " + TABLE1+ "(" +C_ID 
         + " INTEGER PRIMARY KEY AUTOINCREMENT," +Name+ " text unique not null)"); 

       db.execSQL("CREATE TABLE " + TABLE2+ "(" +B_ID 
         + " INTEGER PRIMARY KEY AUTOINCREMENT," +Description+ " text," 
         +Amount+ " text, FOREIGN KEY ("+Description+") REFERENCES "+TABLE1+"("+Name+"));"); 




    } 
+0

「不工作」是對發生了什麼事情的可怕解釋。你是否遇到異常?怎麼了?還包括一個語言標籤(java?)。什麼是DBhelper?你在用什麼庫? –

+0

它沒有給出任何例外,當我單擊按鈕添加值時,它說「更新成功」,並且值也沒有被添加到數據庫中。 DBhelper是我的數據庫類 – xyz

+0

你爲什麼要檢查'DBhelper.Amount == null'。這不是一個靜態字符串變量嗎? – pgiitu

回答

1

嘗試使用這樣的檢查,如果給定name一行出現在table1

public boolean checkIfRowPresent(String name) { 
    SQLiteDatabase db = helper.getWritableDatabase(); 

    Cursor cursor = 
      db.query(DBhelper.TABLE1, null, DBHelper.NAME + "='" + name + "'", null, null, null, 
        null, null); 
    boolean ret = false; 
    if (cursor.getCount() > 0) { 
     ret = true; // There is a row present in table1 with the given name 
    } 
    db.close(); 

    return ret; 

} 

你應該這樣來調用它:

checkIfRowPresent(txr.getText().toString())

讓我知道它是否有幫助?

+0

好的,我該如何檢查一下列是否爲空?你能否簡單地解釋一下plz – xyz

+0

檢查我編輯的答案。 – pgiitu

+0

對不起,延遲迴復,非常感謝,你的回答完美無缺。 – xyz

相關問題