为什么我的SQLite数据库没有执行更新?

时间:2014-11-17 05:29:14

标签: java android sqlite sql-update android-sqlite

我有表名为'Allocated_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;
            }
        }

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

2 个答案:

答案 0 :(得分:2)

 SQLiteDatabase.update() returns number of rows affected. So,
 in your case, no rows were affected (most probably because of your WHERE clause)

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

答案 1 :(得分:1)

您的where子句未正确形成。修理它。应该连接字符串以形成具有所有条件的where子句。

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

应该是下面的内容

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