SQLite小试

前端之家收集整理的这篇文章主要介绍了SQLite小试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.安装

遵照不般的安装步骤:

$./configure

$make

$su -c "make install"

注意执行make install只有root权限才能把sqlite编译得到的文件安装到默认的目录/usr/local下。如果想更改安装路径可以用

$./configure --prefix=/opt/sqlite

其他的安装选项可以输入$./configure --help进行查看。

2.检查一下所安装的头文件、库、可执行程序

orisun@zcypc:~$ ll /usr/local/include/*sqlite*#Linux默认从/usr/local/include下去寻找头文件

-rw-r--r-- 1 root root 20686 2011-01-12 16:05 /usr/local/include/sqlite3ext.h

-rw-r--r-- 1 root root 295268 2011-01-12 16:05 /usr/local/include/sqlite3.h

orisun@zcypc:~$ ll /usr/local/lib/*sqlite*

-rw-r--r-- 1 root root 1891672 2011-01-12 16:05 /usr/local/lib/libsqlite3.a

-rwxr-xr-x 1 root root 828 2011-01-12 16:05 /usr/local/lib/libsqlite3.la*

lrwxrwxrwx 1 root root 19 2011-01-12 16:05 /usr/local/lib/libsqlite3.so -> libsqlite3.so.0.8.6*

lrwxrwxrwx 1 root root 19 2011-01-12 16:05 /usr/local/lib/libsqlite3.so.0 -> libsqlite3.so.0.8.6*

-rwxr-xr-x 1 root root 1650714 2011-01-12 16:05 /usr/local/lib/libsqlite3.so.0.8.6*

orisun@zcypc:~$ ll /usr/local/bin/*sqlite*

-rwxr-xr-x 1 root root 93138 2011-01-12 16:05 /usr/local/bin/sqlite3*

3.安装完成后输入$sqlite3 test.db进行测试

orisun@zcypc:~$ sqlite3 test.db

sqlite version 3.7.4

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

... ...sqlite> .exit

4.简单的sqlite3数据库操作由3个函数组成:sqlite3_open(),sqlite3_exec(),sqlite3_close()

#include<sqlite3.h>
#include<stdio.h>
#include<stdlib.h>			//exit

static int select_callback(void *NotUsed,int argc,char **argv,char **azColName){
	int i;
	for(i=0;i<argc;i++){
		printf("%s=%s\n",azColName[i],argv[i]?argv[i]:"NULL");
	}
	printf("\n");
	return 0;
}
int main(){
	sqlite3 *db;
	char *zErrMsg=0;
	int rc;

	rc=sqlite3_open("test.db",&db);		//will create a new db if not existing
	if(rc!=sqlITE_OK){
		fprintf(stderr,"can't open database:%s\n",sqlite3_errmsg(db));
		sqlite3_close(db);
		exit(1);
	}
	char *sql="create table T1(id integer primary key,name varchar(20))";
	rc=sqlite3_exec(db,sql,&zErrMsg);
	if(rc==sqlITE_OK){
		printf("Table T1 created.\n");
	}
	else{
		fprintf(stderr,"sql error:%s\n",zErrMsg);
	}
	sql="insert into T1 values(101,'Tom')";
	rc=sqlite3_exec(db,&zErrMsg);
	if(rc==sqlITE_OK){
		printf("One record inserted:(101,'Tom').\n");
	}
	else{
		fprintf(stderr,zErrMsg);
	}
	sql="select id,name from T1";
	rc=sqlite3_exec(db,select_callback,&zErrMsg);
	if(rc!=sqlITE_OK){
		fprintf(stderr,zErrMsg);
	}
	sqlite3_close(db);
	return 0;
}

sqlite3_exec(db,&zErrMsg);执行查询每返回一条记录(注意并不是每执行一次查询)都会调用一次回调函数

int (*callback)(void*,int,char**,char**)

第2个参数int查询所得到的列数numbers of columns

第3个参数是字符串数组,每一个字符串都是查询所得到的值

第4个参数是字符串数组,每一个字符串都是列名,即在创建表单时所指定的列名。

5.编译运行

由于sqlite默认安装到了/usr/local下,所以用gcc编译时头文件路径和库文件路径都不需要指定了,只需要用-l指定sqlite的库即可。

orisun@zcypc:~/Program$ gcc sqlite3.c -lsqlite3 -o sqlite3

orisun@zcypc:~/Program$ ./sqlite3

Table T1 created.

One record inserted:(101,'Tom').

id=101

name=Tom

猜你在找的Sqlite相关文章