sqlite3在Linux下的安装和使用

from: http://blog.163.com/18_2/blog/static/2174448200851045114653/

sqlite3在Linux下的安装和使用



自我补充:ubuntu在线安装sqlite3数据库的方法:  系统平台:ubuntu12.04

在ubuntu里面直接使用命令:sudo apt-get install sqlite3 ,出现:
…………
…………
libsqlite3-0 (= 3.7.9-2ubuntu1) but 3.7.9-2ubuntu1.1 is to be installed……
…………
…………
我的解决方法依次执行下面三条命令:
1、sudo dpkg --force-depends --purge libsqlite3-0:i386
2、sudo apt-get -f install
3、sudo apt-get install sqlite3 

Read more: How to Install SQLite 3 in Ubuntu | eHow http://www.ehow.com/how_8463512_install-sqlite-3-ubuntu.html#ixzz2U4yhrano

http://askubuntu.com/questions/230619/cannot-install-sqlite3



2008-06-10 16:51:14| 分类: sqlite |字号 订阅

一.本地sqlite3的安装和配置


下载sqlite3源码包

tar xvfz sqlite-src-3.3.5

cd sqlite-3.3.5

./configure –no-tcl

make

(如果在arm板上用需修改Makefile,在LIBS 项中加入 –L/usr/local/arm/3.3.2/lib 保存后退出.然后再make)



安装成功后生成文件将被复制到指定目录

再加入一个环境变量



vi /etc/profile

在export区域加入 SQLITE3_PATH=/usr/local/sqlite-3.3.5 保存后退出

source /etc/profile



有用文件分别为 $SQLITE3_PATH/include/sqlite3.h 头文件

$SQLITE3_PATH/bin/sqlite3 可执行文件

$SQLITE3_PATH/lib/libsqlite3.a

libsqlite3.la

libsqlite3.so -> libsqlite3.so.0.8.6

libsqlite3.so.0 -> libsqlite3.so.0.8.6

libsqlite3.so.0.8.6



在程序使用到数据库

#include



在.pro文件中添加:

INCLUDEPATH +=$(SQLITE3_PATH)/include

DEPENDPATH +=$(SQLITE3_PATH)/include

LIBS +=-L$(SQLITE3_PATH)/lib

LIBS +=-lsqlite3

保存后退出

tmake hello.pro –o Makefile

make



二.程序的编写:


sqlite3 *db;

int rc = sqlite3_open(“data.db”,&db); //打开数据库data.db

if(rc!=SQLITE_OK);//打开失败



创建表rc = sqlite3_exec(db,”create table student(ID integer

primarykey,name nvarchar(32))”,NULL,NULL,&mrrmsg);



插入rc = sqlite3_exec(db,”insert into student values(‘3’,’Zhang San’)”,0,0,&mrrmsg);





修改rc = sqlite3_exec(db,”update student set name=’Li Si’ where ID=’4’”,0,0,&mrrmsg);



删除rc = sqlite3_exec(db,”delete * from student where name=’Wang Wu’”,0,0,&mrrmsg);



查询是重点

回调式查询 rc = sqlite3_exec(db,”select * from student where ID>2

and name<>’Zhang San’”,LoadInfo,NULL,&mrrmsg);

查询的结果通过LoadInfo函数来导出



typedef int(*sqlite3_callback)(void *,int char ** ,char **);

int LoadInfo(void *para, //db

int n_column, //记录的列数目

char** column_value,//查出值

char** column_name)//字段名称

{

int i;

printf(“记录含%d个字段\n”,n_column);

for(i=0;i
printf(“字段名:%s , 字段值:%s \n”,column_name[i],column_value[]i);

return 0;

}



函数式查询 rc = sqlite_get_table(db,”select ID from student where ID>2

and name<>’Zhang San’”,&result,&nrow,&ncolumn,&errmsg);



char** result;

int nrow,ncolumn;

int rc;

char* errmsg=NULL;

rc = sqlite_get_table(db,”select * from student where ID>2

and name<>’Zhang San’”,&result,&nrow,&ncolumn,&errmsg);

int i;

char id[10];

char name[10];

for(int i=1;i<=nrow;i++)

{

id[i]=result[i*2];

name[i]=result[i*2+1];

}

三.sqlite3命令:


sqlite3 data.db //打开data.db

.tables 查看数据库中包含表的信息

.schema 输出格式

.mode line / column /list //格式 单行,列,列表

.timeout //用时

.output test.txt //输出到test.txt

.q退出

可执行一般的SQL语句

[root@localhost root]#sqlite3 data.db

sqlite>create table student(ID integer primary key,name nvarchar(32));

sqlite>insert into student values(1,’Sun Wukong’);

sqlite>insert into student values(2,’Tang Seng’);

sqlite>select * from student;

1|Sun Wukong

2|Tang Seng

sqlite>update student set name=’Zhu Bajie’ where ID=’2’;

sqlite>select * from student;

1|Sun Wukong

2|Zhu Bajie

sqlite>delete from student where ID=’2’ and name=’ Zhu Bajie’;

sqlite>select * from student;

1|Sun Wukong

sqlite>.tables

student



对中文的支持

[root@localhost root]#sqlite3 data.db

sqlite>create table student(ID integer primary key,name text);//注意:此处name类型为text

sqlite>insert into student values(1,’孙悟空’);

sqlite>insert into student values(2,’唐僧’);

sqlite>select * from student;

1|孙悟空

2|唐僧

sqlite>update student set name=’猪八戒’ where ID=’2’;

sqlite>select * from student;

1|孙悟空

2|猪八戒

sqlite>delete from student where ID=’2’ and name=’ 猪八戒’;

sqlite>select * from student;


你可能感兴趣的:(SQLite数据库)