android 的replace,android-如何正确使用INSERT或REPLACE?

我原本是在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.

标签:sqlite,android

来源: https://codeday.me/bug/20191102/1990496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值