简体   繁体   English

在Sqlite中更新表时出错

[英]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 标签数据库sqlite返回:错误代码= 1,味精=靠近“ =“:语法错误

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=? 03-27 15:15:29.291:E / Database(12011):更新date_of_birth = 2012年3月27日时发生错误animaltype =小牛性别= F location = Eastn dam = 601 animal_id = 601A使用UPDATE cow SET date_of_birth = ?, animaltype =? ,sex = ?、 location = ?、 dam = ?、 animal_id =? WHERE _ID= _ID =

I've obviously got a problem with the value for _ID but can't seem to locate it. 我显然对_ID的值有问题,但似乎找不到它。 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. 发生问题是因为我无法传递我想要更新的记录(_ID)的实际值。 Once I passed that as a parameter to my updaterecords function the update went as scheduled. 一旦将其作为参数传递给我的updaterecords函数,更新就会如期进行。

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 : 检查您的数据库创建,您可能有一个名为_id的列(尽管您通过_ID来引用它,其名称是_id )而不是_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: 编辑:在您的where选择参数中:

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: 您可能想要在其中添加从某处获得的一些ID(要更新的记录,很long数字),现在您正在执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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