sqlite常用语法

在终端下运行sqlite3 <*.db>,出现如下提示符:
SQLite version 3.7.2
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite>
<*.db> 是要打开的数据库文件。若该文件不存在,则自动创建。 
显示所有命令 
sqlite> .help
退出sqlite3
sqlite>.quit
显示当前打开的数据库文件 
sqlite>.database
显示数据库中所有表名 
sqlite>.tables
查看表的结构 
sqlite>.schema <table_name>
/*******************************************/ 
以下为SQL命令,每个命令以;结束 
创建新表 
>create table <table_name> (f1 type1, f2 type2,…);
sqlite> create table student(no integer primary key, name text, score real); 
删除表 
sqlite>drop table <table_name>
sqlite>drop table student 
查询表中所有记录 
sqlite>select * from <table_name>; 
按指定条件查询表中记录 
sqlite>select * from <table_name> where <expression>; 
sqlite> select * from student 
sqlite> select * from student where name=’zhao’
sqlite> select * from student where name=’zhao’ and score >=95
sqlite> select count(*) from student where score>90 
向表中添加新记录 
sqlite>insert into <table_name> values (value1, value2,…);
sqlite> insert into student values(1, ‘zhao’, 92); 
按指定条件删除表中记录 
sqlite>delete from <table_name> where <expression>
sqlite> delete from student where score<60; 
更新表中记录 
sqlite>update <table_name> set <f1=value1>, <f2=value2>… where <expression>; 
sqlite> update student set score=0;
sqlite> update student set name=’sun’ where no=3; 
在表中添加字段 
sqlite>alter table <table> add column <field> <type>; 
sqlite> alter table student add column gender integer default 0; 
在表中删除字段 
Sqlite中不允许删除字段,可以通过下面步骤达到同样的效果
sqlite> create table stu as select no, name, score from student
sqlite> drop table student
sqlite> alter table stu rename to student

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章