sqlite命令使用

系统平台:ubuntu10.04
参考:http://blog.sina.com.cn/s/blog_74dfa9f401017s69.html
简介
sqlite3一款主要用于嵌入式的轻量级数据库,本文旨在为熟悉sqlite3基本命令提供技术文档。
     备注:本文所有操作均在root用户下进行。

1、安装sqlite3
ubuntu下安装sqlite3直接在终端运行命令:
#apt-get install sqlite3
查看版本信息:
#sqlite3 -version
 
sqlite3常用命令
当前目录下建立或打开test.db数据库文件,并进入sqlite命令终端,以sqlite>前缀标识:
#sqlite3 test.db
 
查看数据库文件信息命令(注意命令前带字符'.')
sqlite>.database

查看所有表的创建语句:
sqlite>.schema
 
查看指定表的创建语句:
sqlite>.schema table_name
 
sql语句的形式列出表内容:
sqlite>.dump table_name
 
设置显示信息的分隔符:
sqlite>.separator symble
Example:设置显示信息以‘:’分隔
sqlite>.separator :
 
设置显示模式:
sqlite>.mode mode_name
Example:默认为list,设置为column,其他模式可通过.help查看mode相关内容
sqlite>.mode column
 
输出帮助信息:
sqlite>.help
 
设置每一列的显示宽度:
sqlite>.width width_value
Example:设置宽度为2
sqlite>.width 2
 
列出当前显示格式的配置:
sqlite>.show
 
退出sqlite终端命令:
sqlite>.quit

sqlite>.exit
 
3sqlite3指令
sql的指令格式:所有sql指令都是以分号(;)结尾,两个减号(--)则表示注释。
如:
sqlite>create studen_table(Stu_no interger PRIMARY KEY, Name text NOT NULL, Id interger UNIQUE, Age interger CHECK(Age>6), School text DEFAULT 'xx小学);
该语句创建一个记录学生信息的数据表。
 
3.1 sqlite3存储数据的类型
NULL:标识一个NULL
INTERGER:整数类型
REAL:浮点数
TEXT:字符串
BLOB:二进制数

3.2 sqlite3存储数据的约束条件
Sqlite常用约束条件如下:
PRIMARY KEY 主键:
1)主键的值必须唯一,用于标识每一条记录,如学生的学号
2)主键同时也是一个索引,通过主键查找记录速度较快
3)主键如果是整数类型,该列的值可以自动增长
NOT NULL 非空:
约束列记录不能为空,否则报错
UNIQUE 唯一:
除主键外,约束其他列的数据的值唯一
CHECK 条件检查:
约束该列的值必须符合条件才可存入
DEFAULT 默认值:
列数据中的值基本都是一样的,这样的字段列可设为默认值

3.3 sqlite3常用指令
1)建立数据表
create table table_name(field1 type1, field2 type1, ...);
table_name是要创建数据表名称,fieldx是数据表内字段名称,typex则是字段类型。
例,建立一个简单的学生信息表,它包含学号与姓名等学生信息:
create table student_info(stu_no interger primary key, name text);
 
2)添加数据记录
insert into table_name(field1, field2, ...) values(val1, val2, ...);
valx为需要存入字段的值。
例,往学生信息表添加数据:
Insert into student_info(stu_no, name) values(0001, alex);
 
3)修改数据记录
update table_name set field1=val1, field2=val2 where expression;
wheresql语句中用于条件判断的命令,expression为判断表达式
例,修改学生信息表学号为0001的数据记录:
update student_info set stu_no=0001, name=hence where stu_no=0001;
 
4)删除数据记录
delete from table_name [where expression];
不加判断条件则清空表所有数据记录。
例,删除学生信息表学号为0001的数据记录:
delete from student_info where stu_no=0001;
 
5)查询数据记录
select指令基本格式:
select columns from table_name [where expression];
a查询输出所有数据记录
select from table_name;
b限制输出数据记录数量
select from table_name limit val;
c升序输出数据记录
select from table_name order by field asc;
d降序输出数据记录
select from table_name order by field desc;
e条件查询
select from table_name where expression;
select from table_name where field in ('val1', 'val2', 'val3');
select from table_name where field between val1 and val2;
f查询记录数目
select count (*) from table_name;
g区分列数据
select distinct field from table_name;
有一些字段的值可能会重复出现,distinct去掉重复项,将列中各字段值单个列出。
 
6)建立索引
当说数据表存在大量记录,索引有助于加快查找数据表速度。
create index index_name on table_name(field);
例,针对学生表stu_no字段,建立一个索引:
create index student_index on student_table(stu_no);
建立完成后,sqlite3在对该字段查询时,会自动使用该索引。
 
7)删除数据表或索引
drop table table_name;
drop index index_name;

参考资料:
http://www.sqlite.com.cn/MySqlite/4/378.Html
http://www.cnblogs.com/myqiao/ 


实战:
ygm@ubuntu:/mnt/hgfs/ubuntu-share/sqlite3/source$ sqlite3 ygm_db.db 
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .help
.backup ?DB? FILE      Backup DB (default "main") to FILE
.bail ON|OFF           Stop after hitting an error.  Default OFF
.databases             List names and files of attached databases
.dump ?TABLE? ...      Dump the database in an SQL text format
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.echo ON|OFF           Turn command echo on or off
.exit                  Exit this program
.explain ?ON|OFF?      Turn output mode suitable for EXPLAIN on or off.
                         With no args, it turns EXPLAIN on.
.header(s) ON|OFF      Turn display of headers on or off
.help                  Show this message
.import FILE TABLE     Import data from FILE into TABLE
.indices ?TABLE?       Show names of all indices
                         If TABLE specified, only show indices for tables
                         matching LIKE pattern TABLE.
.load FILE ?ENTRY?     Load an extension library
.log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
.mode MODE ?TABLE?     Set output mode where MODE is one of:
                         csv      Comma-separated values
                         column   Left-aligned columns.  (See .width)
                         html     HTML <table> code
                         insert   SQL insert statements for TABLE
                         line     One value per line
                         list     Values delimited by .separator string
                         tabs     Tab-separated values
                         tcl      TCL list elements
.nullvalue STRING      Print STRING in place of NULL values
.output FILENAME       Send output to FILENAME
.output stdout         Send output to the screen
.prompt MAIN CONTINUE  Replace the standard prompts
.quit                  Exit this program
.read FILENAME         Execute SQL in FILENAME
.restore ?DB? FILE     Restore content of DB (default "main") from FILE
.schema ?TABLE?        Show the CREATE statements
                         If TABLE specified, only show tables matching
                         LIKE pattern TABLE.
.separator STRING      Change separator used by output mode and .import
.show                  Show the current values for various settings
.stats ON|OFF          Turn stats on or off
.tables ?TABLE?        List names of tables
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.
.timeout MS            Try opening locked tables for MS milliseconds
.width NUM1 NUM2 ...   Set column widths for "column" mode
.timer ON|OFF          Turn the CPU timer measurement on or off
sqlite> .tables
stu_info
sqlite> select * from stu_info
   ...> ;
1001|ygm|bj|80
1002|lli|jnan|90
sqlite> .d user_table

wbq.sql文件内容:
BEGIN TRANSACTION;
CREATE TABLE Cars(Id integer PRIMARY KEY, Name text, Cost integer);
INSERT INTO Cars VALUES(1,'Audi',52642);
INSERT INTO Cars VALUES(2,'Mercedes',57127);
INSERT INTO Cars VALUES(3,'Skoda',9000);
INSERT INTO Cars VALUES(4,'Volvo',29000);
INSERT INTO Cars VALUES(5,'Bentley',350000);
INSERT INTO Cars VALUES(6,'Citroen',21000);
INSERT INTO Cars VALUES(7,'Hummer',41400);
INSERT INTO Cars VALUES(8,'Volkswagen',21600);
COMMIT;


建立空的xxx.db数据库
ygm@ubuntu:/mnt/hgfs/ubuntu-share/sqlite3/source$ sqlite3 xxx.db 
sqlite>.read wbq.sql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值