gpt4 book ai didi

java - 为什么我无法更新sqlite数据库的值

转载 作者:行者123 更新时间:2023-11-30 07:52:25 27 4
gpt4 key购买 nike

  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数据库添加和更新值的代码,在我的代码中包含Update方法后,我的添加功能不起作用,我的更新功能也不起作用。我的代码有什么问题吗,但我没有收到任何错误消息。

这是我的数据库表

     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+"));");




}

最佳答案

尝试使用类似的方法来检查 table1 中是否存在具有给定 name 的行:

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())

请告诉我是否有帮助?

关于java - 为什么我无法更新sqlite数据库的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com