SQLite数据库操作实践
SQLite存储类型
null - 空值
integer - 有符号整数,自增列定义 id integer primary key autoincrement
real - 浮点数,存储为8-byte IEEE浮点数
text - 文本字符串 使用数据库编码(UTF-8, UTF-16BE 或 UTF-16LE)进行存储
blob - 二进制数据,如图片、声音等等
也可以接受其它数据类型例如datetime,varchar,nvarchar,decimal,float,double,bigint 等等
Last login: Tue Jan 7 19:05:33 2014 from 192.168.10.1
[myth@host10 ~]$ su
Password:
[root@host10 myth]# pwd //显示当前工作目录
/home/myth
进入SQLite命令行创建数据库并且打开数据库
[root@host10 myth]# sqlite3 bookstore.db
SQLite version 3.6.20
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main /home/myth/bookstore.db
sqlite>
sqlite> .quit //退出SQLite命令行环境
[root@host10 myth]#
打开数据库
[root@host10 myth]# sqlite3 bookstore.db //如果数据库已经存在了,则打开数据库
SQLite version 3.6.20
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.
.genfkey ?OPTIONS? Options are:
--no-drop: Do not drop old fkey triggers.
--ignore-errors: Ignore tables with fkey errors
--exec: Execute generated SQL immediately
See file tool/genfkey.README in the source
distribution for further information.
.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
.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
.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 NUM NUM ... Set column widths for "column" mode
.timer ON|OFF Turn the CPU timer measurement on or off
sqlite>
创建表
sqlite> create table books(id integer primary key, title nvarchar(50),pubdate datetime);
sqlite> create table publishers(id integer primary key, name varchar(20));
sqlite> .tables //查看数据库中存在的表
books publishers
sqlite> .schema books //显示表结构
CREATE TABLE books(id integer primary key, title nvarchar(50),pubdate datetime);
sqlite>
sqlite> alter table books add column publisher text not null default '' collate nocase; //增加表字段
sqlite> .schema books //显示表结构
CREATE TABLE books(id integer primary key, title nvarchar(50),pubdate datetime, publisher text not null default '' collate nocase);
sqlite>
sqlite> alter table publishers rename to authors; //修改表名
sqlite> .tables //查看数据库中存在的表
authors books
sqlite>
修改字段名称
//修改字段名称和数据类型(删除字段也类似),更简单的做法:如果没有重要数据,可以直接删除表重新创建
sqlite> alter table authors rename to authorsOld;
sqlite> create table authors (id integer primary key, author nvarchar(20));
sqlite> insert into authors select id, name from authorsOld;
sqlite> drop table authorsOld;
sqlite> .schema authors
CREATE TABLE authors (id integer primary key, author nvarchar(20));
//还可以把数据导出成insert语句,然后删除表,重新创建表,执行所有insert语句添加数据
显示数据库所有表名称
sqlite> select name from sqlite_master where type='table' order by name; //显示数据库所有表名称,sqlite_master表是只读的
authors
books
sqlite> select name from
...> (SELECT * from sqlite_master union all select * from sqlite_temp_master)
...> where type='table' order by name; //显示数据库所有表名称(永久和临时)
authors
books
sqlite>
删除表
sqlite> drop table authors;
sqlite> .tables //查看数据库中存在的表
books
sqlite>
往表中插入数据
sqlite> insert into books(id,title,pubdate,publisher) values(1,'交换机.路由器.防火墙','2007-08-28','电子工业出版社');
sqlite> insert into books(id,title,pubdate,publisher) values(2, '网络硬件搭建与配置实践','2007-01-20','电子工业出版社');
sqlite>
sqlite> insert into books(title,pubdate,publisher) values('英语沙龙 ENGLISH SALON 实战版','2005-12-31','世界知识出版社');
sqlite> insert into books(title,pubdate,publisher) values('英语沙龙 ENGLISH SALON 朗读版','2006-12-20','世界知识出版社');
sqlite>
sqlite> insert into books
...> select 100,'软件项目管理理论与案例分析','2007-08-30','中国电力出版社' union all
...> select 105,'初级会计电算化','2006-03-27','中国财政经济出版社';
sqlite>
sqlite> insert into books(id,title,pubdate,publisher)
...> select 300,'供应链管理 ------战略、规划与运营','2003-02-28','社会科学文献出版社' union all
...> select 301,'UML实战教程 ------面向.NET开发人员','2006-12-31','清华大学出版社' union all
...> select 302,'Project 2003 企业项目管理 快译通','2007-02-28','人民邮电出版社';
sqlite>
sqlite> select * from books;
1|交换机.路由器.防火墙|2007-08-28|电子工业出版社
2|网络硬件搭建与配置实践|2007-01-20|电子工业出版社
3|英语沙龙 ENGLISH SALON 实战版|2005-12-31|世界知识出版社
4|英语沙龙 ENGLISH SALON 朗读版|2006-12-20|世界知识出版社
100|软件项目管理理论与案例分析|2007-08-30|中国电力出版社
105|初级会计电算化|2006-03-27|中国财政经济出版社
300|供应链管理 ------战略、规划与运营|2003-02-28|社会科学文献出版社
301|UML实战教程 ------面向.NET开发人员|2006-12-31|清华大学出版社