On 02/19/12 16:59, Jörgen Hägglund wrote:
Hi all!
I'm not sure if it's my brain melting or what it might be... :-)
I have a table defined as:
CREATE TABLE IF NOT EXISTS History (Path TEXT UNIQUE NOT NULL, Hits INTEGER DEFAULT 0)

Is there any way of making inserting data as follows;
- If Path does not exist in the table;
INSERT INTO History VALUES ('c:\', 1)
Should yield:
c:\,1

- But, if Path already exists do this:
UPDATE History SET Hits = Hits + 1 WHERE Path = 'c:\'
Should yield:
c:\,2

Of course, the 'c:\' is entered programmatically (using params).

Anyone up for modifying, explaining and solving this? :-)
This seems to work but I do not fully understand why ;-)
sqlite> .header on
sqlite> CREATE TABLE IF NOT EXISTS History (Path TEXT UNIQUE NOT NULL, Hits INTEGER); sqlite> insert or replace into History (Path, Hits) VALUES ('c:\', (select count(*)+ifnull(Hits,1) from History where path='c:\'));
sqlite> select * from History;
Path|Hits
c:\|1
sqlite> insert or replace into History (Path, Hits) VALUES ('c:\', (select count(*)+ifnull(Hits,1) from History where path='c:\'));
sqlite> select * from History;
Path|Hits
c:\|2
sqlite> insert or replace into History (Path, Hits) VALUES ('c:\', (select count(*)+ifnull(Hits,1) from History where path='c:\'));
sqlite> select * from History;
Path|Hits
c:\|3

--
mvh Roger


_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to