How do I add a new column in between two columns?

You have two options.
First, you could simply add a new column with the following:

ALTER TABLE {tableName} ADD COLUMN COLNew {type};

Second, and more complicatedly, but would actually put the column where you want it, would be to create the new table with the missing column and a temporary new name:

CREATE TABLE {tempNewTableName} (name TEXT, COLNew {type} DEFAULT {defaultValue}, qty INTEGER, rate REAL);

And populate it with the old data:

INSERT INTO {tempNewTableName} (name, qty, rate) SELECT name, qty, rate FROM OldTable;

Then delete the old table:

DROP TABLE OldTable;

Then rename the new table to have the name of the OldTable:

ALTER TABLE {tempNewTableName} RENAME TO OldTable;

I’d much prefer the second option, as it will allow you to completely rename everything if need be.

Leave a Comment