方法更新SQL数据库

方法更新SQL数据库

本文介绍了与ContentValues​​和更新,方法更新SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与SQLiteDatabase类的Andr​​oid的原生更新法更新我的精简版的SQL数据库。

i would like to update my sql lite database with the native update-method of the SQLiteDatabase class of android.

ContentValues dataToInsert = new ContentValues();
dataToInsert.put("name", "flo");
dataToInsert.put("location", "flotown");
String where = "id" + "=" + id;
try{
    db.update(DATABASE_TABLE, dataToInsert, where, null);
}
catch (Exception e){
    String error =  e.getMessage().toString();
}

但我得到以下错误:android.database.sqlite.SQLiteException:近15:语法错误:在编制:更新mytable的设置位置= ?,名称=? WHERE ID = 2010-09-21 15:05:36.995

but i get following error:android.database.sqlite.SQLiteException: near "15": syntax error: , while compiling: UPDATE mytable SET location=?, name=? WHERE id=2010-09-21 15:05:36.995

我不知道,应该是什么的问题。不知怎的值不到货的SQL语句。我做了几乎相同的插入方法和工作相当精细。

I don´t know what should be the problem. Somehow the values do not arrive in the sql statement. I did nearly the same with the insert method and that worked quite fine.

很多THX,弗洛里安

many thx,florian

推荐答案

您正在使用的更新功能失常。它应该是这样的:

You're using the update function wrong. It should be like this:

String where = "id=?";
String[] whereArgs = new String[] {String.valueOf(id)};

db.update(DATABASE_TABLE, dataToInsert, where, whereArgs);

琴弦的whereArgs阵列中被代入用于每个'?'在那里变量。

The Strings in the whereArgs array gets substituted in for each '?' in the where variable.

IE浏览器。如果你有哪里=NAME =?AND型=?那么第一个?将得到替换whereArgs [0]和第二通过whereArgs [1]

ie. if you had where = "name=? AND type=? then the first '?' would get replaced by whereArgs[0] and the second by whereArgs[1].

这篇关于与ContentValues​​和更新,方法更新SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 14:32