How to add/drop column to SQLite DB

Add Column

sqlite> .schema RegisterInfo
sqlite> CREATE TABLE RegisterInfo(sysSerial BIGINT PRIMARY KEY, RegisterServerURL VARCHAR(500), RegisterState INTEGER, MainURL VARCHAR(500), LicenseKey VARCHAR(500), PassCode VARCHAR(64), IsUnitUnreg INTEGER, HostUnitID INTEGER, StationID INTEGER);

sqlite> alter table RegisterInfo add column test varchar(100);
sqlite> .schema RegisterInfo
CREATE TABLE RegisterInfo(sysSerial BIGINT PRIMARY KEY, RegisterServerURL VARCHAR(500), RegisterState INTEGER, MainURL VARCHAR(500), LicenseKey VARCHAR(500), PassCode VARCHAR(64), IsUnitUnreg INTEGER, HostUnitID INTEGER, StationID INTEGER, test varchar(100));


Drop Column
SQLite not support drop column command
You can:
1.Create a new table
2.Copy old table data to new table
3.Drop old table
4.Rename new table name as old table name

sqlite> CREATE TABLE RegisterInfo_new(sysSerial BIGINT PRIMARY KEY, RegisterServerURL VARCHAR(500), RegisterState INTEGER, MainURL VARCHAR(500), LicenseKey VARCHAR(500), PassCode VARCHAR(64), IsUnitUnreg INTEGER, HostUnitID INTEGER);
sqlite> insert into RegisterInfo_new
   ...> select sysSerial,RegisterServerURL,RegisterState,MainURL,LicenseKey,PassCode,IsUnitUnreg,HostUnitID from RegisterInfo;
sqlite> drop table RegisterInfo;
sqlite> alter table RegisterInfo_new rename to RegisterInfo;

沒有留言:

張貼留言

Install KDE Desktop for Ubuntu 24.04

1. Enter following command to install the KDE-plasma sudo apt install kde-plasma-desktop 2. Disable the login screen 2-1. Create default sdd...