开发者

android SQLite update/insert

开发者 https://www.devze.com 2023-03-30 17:54 出处:网络
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.

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.

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);


The syntax is INSERT OR REPLACE INTO <table> (<columns>) VALUES (<values>), as can be seen in the documentation.

In your case, it would be something like this:

INSERT OR REPLACE INTO table_1 (key, value)
VALUES ('LastSelected', '...')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号