An SQLite question. My app copies a database table using the following:
sql = "CREATE TABLE newtable AS SELECT * FROM oldtable"
My problem is that oldtable has a column called ID that I have set as Unique Primary Key Autoincrement. The above code does create the new table, with the same structure as the old, but the new ID column is not set to Autoincrement.
How do I copy an old table to a new, while maintaining an ID column that autoincrements?
read the sql use to create the original table from sqlite_master (select sql from sqlite_master where type = ‘table’ and name = ‘YOUR ORIGINAL TABLE NAME HERE’)
rename the original table (alter table YOUR ORIGINAL TABLE NAME HERE rename to YOUR TABLE NAME COPY)
run the sql to recreate the original exactly as it was - use sql execute to rerun the CREATE TABLE saved in #1
insert all the data back into it (insert into YOUR ORIGINAL TABLE NAME select * from YOUR TABLE NAME COPY)
should recreate the original table exactly as it was originally defined with all the data
oh. I did verify that the feature is actually in the standard and not an PostgreSQL extension but obviously failed to realise that the question was SQLite specific, which apparently does not support this syntax. Sorry for the noise.