jeudi 10 septembre 2015

How to check whether row was inserted or replaced in sqlite

I have the SQLite database an I created an unique index like this

CREATE UNIQUE INDEX [my_unique_idx] ON [my_table] ([field1], [field2]);

Now in my Qt's program I want to INSERT OR REPLACE a row in my_table and if it was inserted I need to insert other rows in other "slave" table. In the other hand, if the row was updated I need to do nothing. In other words:

if ( query.exec(
    "insert or replace into my_unique_idx"
    "    (field1, field2, other_field)"
    "  values"
    "    (1, 2, 'foo')"
   ) )
{
    if ( query.is_was_realy_inserted() ) // <---- how to ?
    {
        slave_query.exec( "insert into slave_table......
    }
}

I know, that I can do it in two queries: select a row with appropriate values and if row doesn't exist then insert data into both tables ("master" and "slave"), and just update "master" table if row is exists.
But this way makes unique index and INSERT OR REPLACE clause senseless.

Aucun commentaire:

Enregistrer un commentaire