Linux下SQLite数据库安装操作与编程

来源:互联网 发布:js canvas scale 原点 编辑:程序博客网 时间:2024/05/17 08:39

 

 

//----------------------------------------------------

//AUTHOR: lanyang123456

//DATE: 2012-6-20

//----------------------------------------------------

 

环境

Linux 操作系统: Fedora 16

内核:3.1

sqlite 版本:3.7.13

 

目录

1.编译与安装

1.1下载sqlite源码

1.2编译与安装

2.sqlite数据库管理

3.sqlite数据库编程

参考

 


 

1.编译与安装

 

1.1下载sqlite源码

 

官网下载地址

 

http://www.sqlite.org/download.html

选择下载项:

Source Code

sqlite-autoconf-3071300.tar.gz
(1.76 MiB)

 

下载得到文件

sqlite-autoconf-3071300.tar.gz

 

1.2编译与安装

 

解压sqlite压缩文件

tar –zvxf sqlite-autoconf-3071300.tar.gz

得到文件sqlite-autoconf-3071300

 

下面的一些操作参考sqlite源文件中的INSTALL文件,这是一份好的安装说明。

 

进入sqlite-autoconf-3071300目录

[root@localhost ~]# cd /home/sqlite-autoconf-3071300/

配置

[root@localhost sqlite-autoconf-3071300]# ./configure

编译

[root@localhost sqlite-autoconf-3071300]# make

安装

[root@localhost sqlite-autoconf-3071300]# make install

 

 

默认安装路径为/usr/local/及系统标准目录

头文件 sqlite3.h sqlite3ext.h安装在 /usr/local/include

以及头文件标准目录 /usr/include

 

库文件libsqlite3.a  libsqlite3.so.0.8.6   libsqlite3.so.0  libsqlite3.so

安装在/usr/local/lib目录下

并且共享库文件libsqlite3.so libsqlite3.so.0 libsqlite3.so.0.8.6安装在系统库文件标准目录/usr/lib

 

可执行文件sqlite3安装在 /usr/local/bin目录下以及系统可执行标准目录/usr/bin

 

帮助文档man安装在/usr/local/share目录下

 

2.数据库管理

 

创建数据库文件

 

[root@localhost /]# sqlite3 mydbtest

SQLite version 3.7.13 2012-06-11 02:05:22

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite>

出现sqlite>提示符

查看目前的数据库。注意数据库操作命令以.开头。

sqlite> .database

seq  name             file                                                     

---  ---------------  ----------------------------------------------------------

0    main             //mydbtest                                                

sqlite>

列出当前使用的数据库mydbtest

数据库mydbtest文件创建在执行命令# sqlite3 mydbtest时,命令行所在的目录。

 

创建表

sqlite> create table mytable(name varchar(80),num smallint);

 

列出表

查看创建了哪些表

sqlite> .tables

mytable

sqlite>

查找某个表

sqlite> .tables my

sqlite> .tables mytable

mytable

sqlite>

 

插入记录

sqlite> insert into mytable values('lian',19);

sqlite> insert into mytable values('zheng',20);

sqlite> insert into mytable values('Qing',23);

 

查询

sqlite> select * from mytable;

lian|19

zheng|20

Qing|23

sqlite>

 

模式查看表结构

sqlite> .schema

CREATE TABLE mytable(name varchar(80),num smallint);

sqlite> 

 

从文件向表中导入数据

创建文件data.txt,内容如下

 

LTian Hong|19

Eng Lish|20

Gao Yuan|23

Wei Da|26

 

 

其中“|”是分隔符,分隔符左右不要有空格

 

导入数据前查询

sqlite> select * from mytable;

lian|19

zheng|20

Qing|23

sqlite> .import data.txt mytable

 

导入数据后查询

sqlite> select * from mytable;

lian|19

zheng|20

Qing|23

LTian Hong|19

Eng Lish|20

Gao Yuan|23

Wei Da|26

sqlite>

 

生成形成数据库表的SQL脚本

sqlite> .dump mytable

PRAGMA foreign_keys=OFF;

BEGIN TRANSACTION;

CREATE TABLE mytable(name varchar(80),num smallint);

INSERT INTO "mytable" VALUES('lian',19);

INSERT INTO "mytable" VALUES('zheng',20);

INSERT INTO "mytable" VALUES('Qing',23);

INSERT INTO "mytable" VALUES('LTian Hong',19);

INSERT INTO "mytable" VALUES('Eng Lish',20);

INSERT INTO "mytable" VALUES('Gao Yuan',23);

INSERT INTO "mytable" VALUES('Wei Da',26);

COMMIT;

sqlite>

 

数据导出

 

将输出定向到文件

sqlite> .output create.sql

sqlite> .dump mytable

 

将数据库表生成的SQL脚本输出到create.sql文件

 

将输出恢复到标准输出

sqlite> .output stdout

sqlite>

 

打印SQLite环境变量到设置

sqlite> .show

     echo: off

  explain: off

  headers: off

     mode: list

nullvalue: ""

   output: stdout

separator: "|"

    stats: off

    width:

sqlite>

退出数据库,使用.quit.q

sqlite> .quit

[root@localhost mysqlite_databasefile]#

 

 

特殊用法

命令行下直接使用

[root@localhost mysqlite_databasefile]# sqlite3 mydbtest "select * from mytable;"

lian|19

zheng|20

Qing|23

[root@localhost mysqlite_databasefile]#

 

 

3.数据库编程

 

系统头文件标准目录/usr/include下有头文件sqlite3.h  sqlite3ext.h

所以编程时可直接使用 #include <sqlite3.h>包含头文件。

若系统头文件标准目录下没有需要的头文件,则需要将头文件与程序源文件放在同一目录下,并使用#include “sqlite3.h”,或者其它方法。

 

3.1编写源代码

#include <stdio.h>

#include <stdlib.h> //exit等函数的声明

#include <sqlite3.h>

………………

详细代码见程序源文件http://download.csdn.net/detail/lanyang123456/4384399。

3.2编译源代码

[root@localhost mysqlite_databasefile]# gcc -o mysqlite3 mysqlite.c

/tmp/ccuW3QVl.o: In function `inquire_Usecb':

mysqlite.c:(.text+0xa7): undefined reference to `sqlite3_exec'

/tmp/ccuW3QVl.o: In function `inquire_nocb':

mysqlite.c:(.text+0x11d): undefined reference to `sqlite3_get_table'

mysqlite.c:(.text+0x1ab): undefined reference to `sqlite3_free_table'

/tmp/ccuW3QVl.o: In function `createnTable':

mysqlite.c:(.text+0x1ef): undefined reference to `sqlite3_exec'

/tmp/ccuW3QVl.o: In function `insertRecord':

mysqlite.c:(.text+0x23f): undefined reference to `sqlite3_exec'

mysqlite.c:(.text+0x287): undefined reference to `sqlite3_exec'

mysqlite.c:(.text+0x2cf): undefined reference to `sqlite3_exec'

/tmp/ccuW3QVl.o: In function `deleteRecord':

mysqlite.c:(.text+0x334): undefined reference to `sqlite3_exec'

mysqlite.c:(.text+0x381): undefined reference to `sqlite3_get_table'

mysqlite.c:(.text+0x40f): undefined reference to `sqlite3_free_table'

/tmp/ccuW3QVl.o: In function `main':

mysqlite.c:(.text+0x436): undefined reference to `sqlite3_open'

mysqlite.c:(.text+0x44d): undefined reference to `sqlite3_errmsg'

mysqlite.c:(.text+0x474): undefined reference to `sqlite3_close'

mysqlite.c:(.text+0x4e2): undefined reference to `sqlite3_close'

collect2: ld返回 1

 

编译时指定库文件名sqlite3,系统会在库文件默认目录/lib/usr/lib搜索库

[root@localhost mysqlite_databasefile]# gcc -o mysqlite3 mysqlite.c -lsqlite3

3.3执行程序

[root@localhost mysqlite_databasefile]# ./mysqlite3

You have opened a sqlite3 database successfully!

row:4 column = 5

The result of querying is :

azResult[0] = ID

azResult[1] = SensorID

azResult[2] = SiteNum

azResult[3] = Time

azResult[4] = SensorParameter

azResult[5] = 1

azResult[6] = 101

azResult[7] = 261

azResult[8] = 20100314

azResult[9] = 18.9

azResult[10] = 2

……………………

Total column is 5

字段名: ID---->字段值:1

字段名: SensorID---->字段值:101

字段名: SiteNum---->字段值:261

字段名: Time---->字段值:20100314

字段名: SensorParameter---->字段值:18.9

==========================

………………

Total column is 5

字段名: ID---->字段值:3

字段名: SensorID---->字段值:667

字段名: SiteNum---->字段值:290

字段名: Time---->字段值:20110315

字段名: SensorParameter---->字段值:27.0

==========================

Total column is 5

字段名: ID---->字段值:4

字段名: SensorID---->字段值:865

字段名: SiteNum---->字段值:300

字段名: Time---->字段值:20120616

字段名: SensorParameter---->字段值:323.0

==========================

operate failed: near "DELETE ": syntax error

row:4 column:5

After deleting,the result of querying is :

azResult[0] = ID

azResult[1] = SensorID

azResult[2] = SiteNum

azResult[3] = Time

azResult[4] = SensorParameter

azResult[5] = 1

azResult[6] = 101

azResult[7] = 261

azResult[8] = 20100314

…………

[root@localhost mysqlite_databasefile]#

SQL删除语句出现语法错误,并由输出可以得到其错误的原因。经检查发现 DELETE后面多了空格,修改后,执行结果如下

 

[root@localhost mysqlite_databasefile]# ./mysqlite3

You have opened a sqlite3 database successfully!

row:4 column = 5

The result of querying is :

azResult[0] = ID

azResult[1] = SensorID

azResult[2] = SiteNum

azResult[3] = Time

……………………

==========================

Total column is 5

字段名: ID---->字段值:4

字段名: SensorID---->字段值:865

字段名: SiteNum---->字段值:300

字段名: Time---->字段值:20120616

字段名: SensorParameter---->字段值:323.0

==========================

row:3 column:5

After deleting,the result of querying is :

azResult[0] = ID

azResult[1] = SensorID

…………

[root@localhost mysqlite_databasefile]#

成功删除第4条记录

详细执行结果见执行结果文件http://download.csdn.net/detail/lanyang123456/4384399。

 

 

参考 

SQLite 官网

http://www.sqlite.org/

 

SQLite中文网

http://www.sqlite.com.cn/

 

SQLite3使用教学 数据库使用说明

http://www.sqlite.com.cn/MySqlite/4/378.Html

 

嵌入式数据库SQLite的一份教程

http://www.sqlite.com.cn/MySqlite/3/380.Html

 

sqlite3编程笔记 .

http://blog.csdn.net/wl_haanel/article/details/6231417

 

SQLite3 API编程手册

http://www.cnblogs.com/hnrainll/archive/2011/09/08/2170506.html

 

几篇关于嵌入式数据库的简介,包括SQLite  Berkeley DB

http://blog.chinaunix.net/uid/9563036/frmd/23812.html

 

嵌入式数据库SQLite移植到S3C2410的方法 .

http://blog.csdn.net/liuzhidong123/article/details/6827379

 

sqlite嵌入式数据库在arm-linux下的编译全攻略

http://blog.chinaunix.net/uid-9563036-id-352307.html

 

嵌入式数据库sqliteMotorola Coldfire + uclinux下的移植

http://www.sqlite.com.cn/MySqlite/6/379.Html

 

SQLITE3使用总结 windows下编程接口说明

http://hi.baidu.com/llhg/blog/item/0c3c0da89d83d6b3cb130cdf.html

 

sqlite3使用简介  Windows下编程接口说明

http://www.cnblogs.com/kfqcome/archive/2011/06/27/2136999.html

 

Sqlite快速上手使用指南 WindowsSQLite图形界面使用

http://www.cnblogs.com/yjmyzz/archive/2010/02/18/1669210.html

 

Linux configure关于交叉编译的参数设置

http://tech.ccidnet.com/art/2583/20080307/1383653_1.html

 

 转载请注明出处。