Is there a way to write one SQL statement that will insert a new row in a table if a certain string is not found in a column in that same table? Instead of having two write two SQL statements where one checks to see if a user in a table, and the other that either updates the current user if the user is in the table or inserts the user in a new row in the table if the user is not in the table. Anyone now of how to accomplish this in just one SQL statement ? Thanks Steve
Mrs. Brisby wrote:
You should look at the sqlite documentation for REPLACE.
Steve,
This was good advice even if it was a little terse.
Here is an example:
sqlite> create table t (user primary key, data); sqlite> insert into t values ('a', 1); sqlite> insert or replace into t values ('b', 2); sqlite> select * from t; a|1 b|2 sqlite> insert or replace into t values ('b', 3); sqlite> select * from t; a|1 b|3 sqlite>
This works as long as the column containing the string you want to search for is the primary key of the table (like user above). If the primary key doesn't exist, it does an insert. If it does, it replaces that row with the new values.
HTH
Dennis Cote