Sqlite3 Database Upgrade

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.