Change table designation in sqlite
Posted: Mon Nov 01, 2021 6:33 pm
I have a sqlite db with tables like:
Settings (Var Char(32), Value Char(32)) and I want to change it to Settings (Var TEXT PRIMARY KEY, Value TEXT, Level TINYINT).
The table has already data. How can I do it without loosing the data?
I am trying:
But it seems that it is not working every time and the table looses all values. Also, I do not know if the existing table has all columns to insert to the new one, for example, the above example takes for granted that columns Var and Value do exist. Any suggestions?
Settings (Var Char(32), Value Char(32)) and I want to change it to Settings (Var TEXT PRIMARY KEY, Value TEXT, Level TINYINT).
The table has already data. How can I do it without loosing the data?
I am trying:
Code: Select all
DatabaseUpdate(#db, "PRAGMA foreign_keys = 0")
DatabaseUpdate(#db, "BEGIN TRANSACTION")
DatabaseUpdate(#db, "ALTER TABLE Settings RENAME To sqlitemanager_temp_table_177354083607")
DatabaseUpdate(#db, "CREATE TABLE Settings (Var TEXT PRIMARY KEY, Value TEXT, Level TINYINT)")
DatabaseUpdate(#db, "INSERT INTO Settings (Var, Value) Select Var, Value FROM sqlitemanager_temp_table_177354083607")
DatabaseUpdate(#db, "DROP TABLE sqlitemanager_temp_table_177354083607")
DatabaseUpdate(#db, "COMMIT")
DatabaseUpdate(#db, "PRAGMA foreign_keys = 1")