android SQLite update/insert

旧城冷巷雨未停 提交于 2020-01-11 11:18:24

问题


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

回答1:


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', '...')


来源:https://stackoverflow.com/questions/7180441/android-sqlite-update-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!