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
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.