android SQLite 更新/插入 [英] android SQLite update/insert
本文介绍了android SQLite 更新/插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更新我的表中的一行,WHERE key = LastSelected
如果具有该键的行不存在,我想插入它.
I want to UPDATE a row in my table, WHERE key = LastSelected
If a row with that key does not exist, I want to INSERT it.
如果该行已经存在,我可以让 UPDATE 工作,但如果它丢失,它不会 INSERT.
I can get the UPDATE to work if the row already exists, but it will not INSERT if it is missing.
我已经尝试过这些(第一个正确更新,但没有插入):
I have tried these (the first one correctly updates, but does not insert) :
String.format("UPDATE table_1 SET value = '%s' WHERE key = 'LastSelected'", s);
String.format("REPLACE table_1 SET value = '%s' WHERE key = 'LastSelected'", s);
String.format("INSERT OR REPLACE INTO table_1 SET value = '%s' WHERE key = 'LastSelected'", s);
推荐答案
语法是INSERT OR REPLACE INTO <table>(<columns>) VALUES (<values>)
,可以在 文档中看到.
在你的情况下,它会是这样的:
In your case, it would be something like this:
INSERT OR REPLACE INTO table_1 (key, value)
VALUES ('LastSelected', '...')
这篇关于android SQLite 更新/插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文