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.

http://sqlite.org/lang_conflict.html

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...