Search code examples
javaandroidsqlitesql-updateandroid-sqlite

Why my SQLLiteDatabase is not executing Update?


I have table named 'Allocated_Stock', this table is as follows:

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

And when I update I use

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)

the following is a different class

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

Whenever I try to update, I get an error, but firstly I couldn't catch the error. Second of all why is this happening?


Solution

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