I want to copy a row from a table in a database to an identical table in another database. For testing purposes I created this:
CREATE TABLE stuff (absid integer primary key, otherfield string );
and table 'stuff' is as above in two database, testdb1 and testdb2. Then I put two rows into 'stuff' in testdb1. From a command line, I can then copy a row from one db to the other, thus:
prompt> sqlite3 testdb1
sqlite> attach database testdb2 as testdb2;
sqlite> insert into testdb2.stuff select * from stuff where absid=2;
sqlite> ^d
prompt>
So far so good. BUT: it is quite possible in the application where I actually want to do this for real, that there will be key clashes. For example, if I use the above sequence to copy the row back to testdb1, I get:
SQL error: PRIMARY KEY must be unique
What I would like to happen is that when the row is copied, a new unique absid is chosen automatically if there is a conflict. Is there a way I can specify this with a more complex "select * ..." above?
I guess I can get round this by creating another db (in memory, say) with the identical table but without the primary key constraint, and doing the copy in two steps (setting absid to null in between), but I'd prefer a smarter way if one exists.
Thanks,