Create a table in the current database
create table person(id integer,name Text);
Insert two data entries
insert into person values(1,"zhangsan");insert into person values(2,"lisi");
View the current database content
select * from person;
1 | zhangsan
2 | Lisi
1. Add a new column to the table
Add a new sex column
alter table person add column sex Text default 'male';
Query again
1 | zhangsan | male
2 | Lisi | male
The default value of sex is male.
2. delete a column in the table
Now you want to delete the column sex you just added
alter table person drop column sex;
You will get this error
Error: near "Drop": syntax error
SQLite does not support this operation.
What should I do if I need to delete the original columns during Database Upgrade?
Step 1. Create a new table
create table new_person(id integer,name text);
Step2. copy data from the original table
insert into new_person select id,name from person;
Step3. Delete the original table
drop table person;
Query the new_person table again.
1 | zhangsan
2 | Lisi
This simulates the operation to delete columns.
In this case, the fields in the original table are less than those in the database table to be upgraded.
Person (original old table): Only the field ID and name
New_person (updated table): There are three fields: ID, name, and sex (The purpose of the upgrade is to add this column)
You can do this.
insert into new_person select id,name,'female' from person;
'Female 'is the default value of the sex column.