2015-10-16 110 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

相关问题