简体   繁体   中英

Error on Updating Table in Sqlite

When trying to update a record for one of my records I am using this code

    private void UpdateCattleRecord(UpdateCattleRecord updateRecord){
             mDB.beginTransaction();
             String where = "_ID=";
             String[] RecordToUpdate = {Cattle._ID};
             Toast.makeText(this,"Updating Animal "+ RecordToUpdate, Toast.LENGTH_LONG).show();
             try {
                    ContentValues CattleFieldsToUpdate = new ContentValues();
                    CattleFieldsToUpdate.put(Cattle.CATTLE_ANIMALID,updateRecord.getCattleName());
                    CattleFieldsToUpdate.put(Cattle.CATTLE_TYPE, updateRecord.getCattleType());
                    CattleFieldsToUpdate.put(Cattle.CATTLE_LOCATION, updateRecord.getCattleLocation());
                    CattleFieldsToUpdate.put(Cattle.CATTLE_DOB, updateRecord.getCattleDob());
                    CattleFieldsToUpdate.put(Cattle.CATTLE_DAM, updateRecord.getCattleDam());
                    CattleFieldsToUpdate.put(Cattle.CATTLE_SEX, updateRecord.getCattleSex());
                    mDB.update(Cattle.CATTLE_TABLE_NAME,CattleFieldsToUpdate, where, RecordToUpdate);
                    mDB.setTransactionSuccessful();
              } finally {
                 mDB.endTransaction();
             }
    }

My log shows

Tag Database sqlite returned: error code =1, msg = near "=": syntax error

After researching this, I think I have everything in the right place but obviously I don't, when I look at the next error in the log it's of course in 'red' and it shows me all the correct data,

03-27 15:15:29.291: E/Database(12011): Error updating date_of_birth=March 27, 2012 animaltype=Calf sex=F location=Eastern dam=601 animal_id=601A using UPDATE cattle SET date_of_birth=?, animaltype=?, sex=?, location=?, dam=?, animal_id=? WHERE _ID=

I've obviously got a problem with the value for _ID but can't seem to locate it. Can someone please point out where my Syntax error is?

Update

The problem occurred because I was failing to pass the actual value of the record (_ID) that I wanted to update. Once I passed that as a parameter to my updaterecords function the update went as scheduled.

Thanks for the input, it helped me narrow down what I was doing wrong.

Check your database creation, your probably have a column named _id (although you refer to it by _ID , its name is _id ) and not _ID :

String where = "_id= ?"; // ? represent the value from the selection arguments String array

or better:

String where = Cattle._ID + "= ?";

Edit: In your where selection argument you put:

String[] RecordToUpdate = {Cattle._ID};

you probably want to put in there some id you get from somewhere(of the record you want to update, a long number), right now you're doing:

WHERE _ID = _ID (or _id)

and this will fail.

try:

 mDB.update(Cattle.CATTLE_TABLE_NAME,CattleFieldsToUpdate, "_ID="+Cattle._ID, null);

try:

mDB.update(Cattle.CATTLE_TABLE_NAME,CattleFieldsToUpdate, "_ID="+updateRecord.getId(), null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM