0

hey what i want to do is select everything from the table and insert data into it if its not already present in the table or update the data if present in the table. The problem here is if my table is empty there is nothing to select and hence the query is not executed and therefore it neither inserts nor updates my table. NOTE: My insert and update query are written in the select query in if else condition.

user391301
  • 21
  • 1
  • 5

1 Answers1

0

You can use the INSERT OR REPLACE statement. Also, see the following links.

SQLite "INSERT OR REPLACE INTO" vs. "UPDATE ... WHERE"

http://www.sqlite.org/lang_conflict.html

Community
  • 1
  • 1
Garett
  • 15,936
  • 4
  • 53
  • 62
  • The INSERT OR REPLACE statment inserts the values if they do not exist. Otherwise it updates the vales if they already exist. Did you view the links I provided? – Garett Jul 23 '10 at 05:59
  • Sorry for so many questions but i m really new to iphone and objective C platform . yes, n i did see the insert or replace query before but i want to update the entries only if they exist and also the manipulation takes place only after i retrieve all my data from the table. – user391301 Jul 23 '10 at 06:13
  • `INSERT OR REPLACE` isn't what the OP wants - it will *replace*, not *update*, the values if they already exist. He wants `UPSERT`, which doesn't exist in sqlite. See http://stackoverflow.com/questions/418898/sqlite-upsert-not-insert-or-replace/418988#418988 for a solution. – Jesse Beder May 15 '11 at 04:08