1. 下载sqlite压缩包
2. 解压
3. 配置
export PATH=$PATH:/opt/arm-v4t-linux-gnueabi/gcc-4.1.2-glibc-2.5-kernel-2.6.18/bin/
CFLAGS="-O3" CC=/opt/arm-v4t-linux-gnueabi/gcc-4.1.2-glibc-2.5-kernel-2.6.18/bin/arm-v4t-linux-gnueabi-gcc CXX=/opt/arm-v4t-linux-gnueabi/gcc-4.1.2-glibc-2.5-kernel-2.6.18/bin/arm-v4t-linux-gnueabi-g++ ./configure --host=arm-v4t-linux-gnueabi --prefix=/home/sqlite
4. 编译
make
make install
5. 使用
1、创建数据库
按理说第一步是创建一个数据库,我是学电子的,对计算机不了解,所以我不知道mysql是如何存储数据库的。但sqlite将一个数据库存储为一个文件。我们先进下cmd,关掉原来的sqlite3。
在命令提示符下:
sqlite3 newsql.db
然后就会自动跳到sqlite>命令提示符下。我记得在linux下用时候会在当前目录下出现newsql.db文件。但在我所用版本的windows下没有出现。然后我做了些尝试得到如下结果:
sqlite3 newsql.db
.quit
注意:.quit是在sqlite提示符下输入的,代表退出。看目录下还是没有出现数据库文件。
sqlite3 newsql.db
;
.quit
出现了newsql.db文件。冒号加回车,在sqlite中,代表执行一条语句的意思,虽然我只输入了一个冒号加回车,没有输入任何的语句,但结果已是不同。
2、创建一个表
create table mytable(entry1 varchar(10),entry2 int);
不要忘了加冒号。冒号代表一条语句输入完毕。
mytable是我创建的表名。create 和table都是系统关键字。entry1,entry2是两个表项。
varchar(10) int是类型。根据我读到的内容,sqlite是不区分类型,但是我们还是要在创建表时,给他一个类型,以便于将这些代码移植到其他的数据库里面时更加的方便。
3、向表中插入一条记录
insert into mytable values("hello world",10);
插入完了之后才发现是不是超出定义的大小了?我定义的entry1项是varchar(10)型的,说实在的,我不知这个类型确切来讲是什么意思,我猜应该是10个字符的字符串数组吧。如果那样的话我是一定超出了。但既然sqlite是不区分类型的,我想应该没有问题吧。于是我急于看看是不是这样...
4、查询表中内容
select * from mytable;
执行这条语句,会列出mytable中的所有内容。
结果为:
sqlite> select * from mytable;
hello world|10
可见还是都插入进去了。
sqlite> insert into mytable values("goodbye cruel world",20);
sqlite> select * from mytable;
hello world|10
goodbye cruel world|20
也就是说,第一个条目的字符串完全不受限制。
5、sqlite3 newsql.db的规则
我们现在退出,然后重新打开数据库:
sqlite> .quit
F:/sqlite>sqlite3 newsql.db
SQLite version 3.6.23.1
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from mytable;
hello world|10
goodbye cruel world|20
可见sqlite3 newsql.db这个命令规则为:打开名为newsql.db这个数据库,如果不存在则创建一个。
6、查询一个数据库中所有的表名
sqlite数据库中有一个系统建立的表,名为sqlite_master,查询这个表可以得到所有的表。
sqlite> create table my2ndtable(theonlyentry int);
sqlite> insert into my2ndtable values(30);
sqlite> select * from sqlite_master;
table|mytable|mytable|2|CREATE TABLE mytable(entry1 varchar(10),entry2 int)
table|my2ndtable|my2ndtable|3|CREATE TABLE my2ndtable(theonlyentry int)
对于这个表的定义,官方网站的FAQ中给出如下:
CREATE TABLE sqlite_master ( type TEXT, name TEXT, tbl_name TEXT, rootpage INTEGER, sql TEXT );第一个字段类型显然会一直是table,第二个字段是名称分别是mytable和my2ndtable,见上面的结果。第三个字段表名,没弄懂是什么意,想必是所在的表的名字,但是一个表的名字和所在的表名不是一样的吗?第四个字段rootpage,我也不知指什么,这个系统的学过数据库的人应该能知道,有路过的还望告之。第五个字段是创建表的使用的sql语句吧。
7、sqlite的输出模式
默认的输出格式是“列表”。在列表模式下,每条查询结果记录被写在一行中并且每列之间以一个字符串分割符隔开。默认的分隔符是一个管道符号(“|”)。列表符号在当你输出查询结果到另外一个符号处理的程序(如AWK)中去是尤为有用。
sqlite> .mode list
sqlite> select * from mytable;
hello world|10
goodbye cruel world|20
sqlite>
这是正常的模式。
sqlite> .mode csv
sqlite> select * from mytable;
"hello world",10
"goodbye cruel world",20
变化是什么?字符串被加上了引号。
sqlite> .mode column
sqlite> select * from mytable;
hello world 10
goodbye cru 20
哎呀,第二个字符串被截断了。
.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
这些来自.help命令的输出结果。
sqlite> .mode html
sqlite> select * from mytable;
<TR><TD>hello world</TD>
<TD>10</TD>
</TR>
<TR><TD>goodbye cruel world</TD>
<TD>20</TD>
</TR>
.mode html是一种较新的输出方法。
另外,我们也可以把输出结果输出到文件:
sqlite> .output output.txt
sqlite> select * from mytable;
sqlite> .exit
F:/sqlite>type output.txt
hello world|10
goodbye cruel world|20
8、查看数据库中所有的表
sqlite> .tables
my2ndtable mytable
9、查看所有的表的创建语句
sqlite> .schema
CREATE TABLE my2ndtable(theonlyentry int);
CREATE TABLE mytable(entry1 varchar(10),entry2 int);
sqlite> .schema mytable
CREATE TABLE mytable(entry1 varchar(10),entry2 int);
10、数据库导出和导入
我们可以利用这个功能做一个简单的备份,或是说创建一个同样的数据库。
第一步,把数据库倒出来:
cmd命令提示符下:
F:/sqlite>sqlite3 newsql.db ".dump" >a.sql
此语句将数据库导出成a.sql数据库语句文件,执行这个文件就可以创建一个一模一样数据库:
F:/sqlite>sqlite3 copied.db
SQLite version 3.6.23.1
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from mytable;
hello world|10
goodbye cruel world|20
11、删除记录
delete from mytable where entry2=10;
可以删掉mytable中所有entry2项为10的条目。
注意:不是delete * from mytable,而delete from mytable.没有*.