gpt4 book ai didi

java - 为什么我的 SQLLiteDatabase 没有执行更新?

转载 作者:行者123 更新时间:2023-12-01 22:35:16 25 4
gpt4 key购买 nike

我有一个名为“Allocation_Stock”的表,该表如下:

Create Table Allocated_Stock (Store_Id string not null,Item_Id string not null,
Vehicle_Id string not null, AllocatedDate date not null,Item_Qty decimal not null,

PRIMARY KEY (Store_Id, Item_Id),
FOREIGN KEY (Store_Id) REFERENCES Store (Store_Id),
FOREIGN KEY (Item_Id) REFERENCES Item (Item_Id),
FOREIGN KEY (Vehicle_Id) REFERENCES Vehicle (Vehicle_Id));

当我更新时我使用

String where = "Store_Id='ST001' And Item_Id = 'IM001' And Vehicle_Id = 'V0001' And AllocatedDate = '2014-08-08'";

ContentValues newValues = new ContentValues();
newValues.put(Store_Id, "S001");
newValues.put(Item_Id, "I001");
newValues.put(Vehicle_Id, "V001");
newValues.put(AllocatedDate, "2014-08-08");
newValues.put(Item_Qty, 550);

DBSchema.obj.updateRecord(newValues, TableName, where)

以下是不同的类

private DatabaseHelper myDBHelper; //which extends SqlLiteHelper
private SQLiteDatabase db;

myDBHelper = new DatabaseHelper(context);
db = myDBHelper.getWritableDatabase();

public boolean updateRecord(ContentValues Content, String TableName,String Where ) {

// Insert it into the database.
try
{

return db.update(TableName, Content, Where, null) != 0;
}
catch(Exception e){
System.out.println("GET SAMPLE VALUE"+e);
return false;
}
}

每当我尝试更新时,都会收到错误,但首先我无法捕获该错误。其次,为什么会发生这种情况?

最佳答案

SQLiteDatabase.update() 返回受影响的行数。所以,在您的情况下,没有行受到影响(很可能是因为您的 WHERE 子句)

       int result = 0;
try{
result = db.update("alarms", values, keyId+" = ? " ,new String[]{row});
Log.d("result", ""+result);
}
catch(Exception e)
{
e.printStackTrace();
}
SYstem.out.println("number of row affected :" + result );

关于java - 为什么我的 SQLLiteDatabase 没有执行更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26966124/

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