我原本是在sqlite中寻找INSERT或UPDATE功能的,但是搜索显示类似的提问者被指出使用INSERT或REPLACE.
我显然误解了它的工作原理,并且由于得到以下sqliteException而显然无法使用WHERE……
08-12 01:38:22.973: ERROR/AndroidRuntime(29242): FATAL EXCEPTION: main
08-12 01:38:22.973: ERROR/AndroidRuntime(29242): android.database.sqlite.sqliteException:
near "WHERE":
Syntax error:
INSERT OR REPLACE INTO SERVERS (
server_name,
service_uri_mobile,
service_uri_wifi,
valid_ssids,
username,password
) VALUES (
'Default',
'http://myserver.com:8790/',
'http://192.168.1.1:8790/',
'[]',
'admin',
'password')
WHERE server_name='Default'
我正在使用的sql字符串如下…
String UpdateString = "INSERT OR REPLACE INTO SERVERS " +
"(server_name,service_uri_mobile,service_uri_wifi,valid_ssids,username,password) VALUES ('" +
locater.getServerName() + "','" +
locater.getServiceUriMobile().toString() + "','" +
locater.getServiceUriWifi().toString() + "','" +
locater.getValidSsids().toString() + "','" +
locater.getUsername() + "','" +
locater.getpassword() + "') " +
"WHERE server_name='" + locater.getServerName() + "'";
我看过this page解释了REPLACE,但不太了解.我将如何重新编写上述sqlite命令,并使其仅尝试替换server_name匹配的记录(即,相当于WHERE子句的记录)?
解决方法:
查看文档(并反映问题下方的注释),当更新(或换句话说)更改唯一列时,应使用REPLACE.以机智:
REPLACE
When a UNIQUE constraint violation occurs, the REPLACE algorithm
deletes pre-existing rows that are causing the constraint violation
prior to inserting or updating the current row and the command
continues executing normally.