1、sqlite3

http://www.sqlite.org/

下载

wget http://www.sqlite.org/2014/sqlite-amalgamation-3080403.zip

tar xvf  http://www.sqlite.org/2014/sqlite-amalgamation-3080403.zip

cd sqlite-amalgamation-3080403

2、写调用main函数和makefile

3、编译运行

打开数据库->查询-> 关闭数据库;

获取表信息2种方式:

a) sqlite3_get_table
SQLITE_API int sqlite3_get_table(
sqlite3 *db, /* An open database */
const char *zSql, /* SQL to be evaluated */
char ***pazResult, /* Results of the query */
int *pnRow, /* Number of result rows written here */
int *pnColumn, /* Number of result columns written here */
char **pzErrmsg /* Error msg written here */
);
SQLITE_API void sqlite3_free_table(char **result);

b)sqlite3_exec

  回调函数,读取一行返回给回调函数处理

sqlite3_exec( db,sql, get_sqlite_table_record, NULL, &err);
SQLITE_API int sqlite3_exec(
sqlite3*, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
main函数
#include <stdio.h>
#include "sqlite3.h" int get_sqlite_table_record( void * para, int n_column, char ** column_value, char ** column_name )
{
int i;
for( i = ; i < n_column; i ++ )
{
printf("%s ,%s\n", column_name[i], column_value[i] );
}
printf("\n-----------------\n");
return ;
} int get_table(const char *databasename,char* sql)
{
sqlite3 *db=NULL;
char *err = ; char **dbResult;
int nRow,nCol;
int rc;
rc = sqlite3_open(databasename, &db);
if(rc)
{
fprintf(stderr, "Can't open database: %s", sqlite3_errmsg(db));
sqlite3_close(db);
return -;
}
else
printf("You have opened a sqlite3 database %s",databasename);
; rc = sqlite3_get_table(db,sql,&dbResult,&nRow,&nCol,&err); if(rc==SQLITE_OK)
{
printf("\nnRow=%d nCol=%d\n",nRow,nCol);
int index = ;
int i = ;
int j = ;
for(i=;i<=nRow;i++)
{
printf("\n");
for(j=;j<=nCol;j++)
{
printf("%s || ",dbResult[index++]);
}
printf("\n");
}
}
sqlite3_free_table( dbResult ); //free table sqlite3_close(db); //close databse
dbResult= NULL;
db=NULL; return ;
} int sqlite_record_call_back(const char *databasename,char *sql)
{
sqlite3 *db=NULL;
char *err = ;
int rc;
//open database if not exist create;
rc = sqlite3_open(databasename, &db);
if(rc)
{
fprintf(stderr, "Can't open database: %s", sqlite3_errmsg(db));
sqlite3_close(db);
return -;
}
else
printf("opened a sqlite3 database %s",databasename);
; rc = sqlite3_exec( db,sql, get_sqlite_table_record, NULL, &err);
if(rc==SQLITE_OK){
printf("\nok\n");
}
else{
printf("\nfailed\n");
return -;
} return ;
} int main( void )
{
int ret = -;
const char sqlitename[]="sqlite_test.db"; //query all table in sqlite_test.db
char sql_table[] = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"; char sql1[] = "select id,url,title from dialinfo";
char sql2[] = "select * from logos"; ret = get_table(sqlitename,sql_table); ret = get_table(sqlitename,sql1);
if(ret==)
printf("\nsusccess get_table\n");
else
printf("\nfailed get_table:%s\n",sql1); ret = -;
ret = sqlite_record_call_back(sqlitename,sql2);
if(ret==)
printf("\nsusccess\n");
else{
printf("\nfailed call_back:%s\n",sql2);
return -;
} return ;
}

 
Makefile
CC=gcc -g -Wall

all: a.out
clean:
rm -rf *.o a.out
a.out: sql.o sqlite3.o
$(CC) -ldl -lpthread -o a.out sqlite_main.o sqlite3.o sql.o: sqlite_main.c
$(CC) -c sqlite_main.c
sqlite3.o: sqlite3.c sqlite3.h
$(CC) -c sqlite3.c pack:
tar jcvf sqlite_demo-`date +%Y%m%d%H%M%s`.tar.gz *.c *.h Makefile *.db

如果已经安装sqlite3的动态链接库

可通过 gcc -g -Wall -ldl -lsqlite3 -lpthread -o a.out sqlite_main.o

 

参考网址:

file:http://files.cnblogs.com/frankwz/demo_sqlite-amalgamation-3080403.zip

sqlite3编译与查询的更多相关文章

  1. sqlite3编译

    1.sqlite3编译: 1.PC版: 1.解压: tar xvf sqlite-autoconf-3140100.tar.gz cd sqlite-autoconf-3140100/ 2.检查配置 ...

  2. 关于SQLite3 编译及交叉编译的一些问题

    from : http://blog.sina.com.cn/s/blog_5f2e119b0101ibwn.html SQLite3 (http://www.sqlite.org)是一个非常强大的小 ...

  3. PHP7预编译mysqli查询操作

    //连接数据库 $mysqli = new mysqli("localhost", "root", "root", "mobile ...

  4. Entity Framework 6 Recipes 2nd Edition(13-6)译 -> 自动编译的LINQ查询

    问题 你想为多次用到的查询提高性能,而且你不想添加额外的编码或配置. 解决方案 假设你有如Figure 13-8 所示的模型 Figure 13-8. A model with an Associat ...

  5. [译]SQL Server 之 查询计划缓存和重编译

    查询优化是一个复杂而且耗时的操作,所以SQL Server需要重用现有的查询计划.查询计划的缓存和重用在多数情况下是有益的的,但是在某些特殊的情况下,重编译一个查询计划可能能够改善性能. SELECT ...

  6. EF Core 使用编译查询提高性能

    今天,我将向您展示这些EF Core中一个很酷的功能,通过使用显式编译的查询,提高查询性能. 不过在介绍具体内容之前,需要说明一点,EF Core已经对表达式的编译使用了缓存:当您的代码需要重用以前执 ...

  7. EntityFramework Core 2.0 Explicitly Compiled Query(显式编译查询)

    前言 EntityFramework Core 2.0引入了显式编译查询,在查询数据时预先编译好LINQ查询便于在请求数据时能够立即响应.显式编译查询提供了高可用场景,通过使用显式编译的查询可以提高查 ...

  8. Ubuntu下sqlite3的安装及使用

    Sqlite是一款轻型的数据库,实现了多数SQL-92标准,包括事务(原子性,一致性,隔离性和持久性 ACID),触发器与多数复杂查询.对于一个移动手持设备的应用开发者,Sqlite是居家旅行必备数据 ...

  9. SQLite3 安装、基本操作

    1. 安装SQLite3 sudo apt-get install sqlite3 2. 安装Sqlite3编译需要的工具包 如果,你需要的话可以安装该工具包.只是为了体验一把,可以不安装.该项是可选 ...

随机推荐

  1. javascript 正则表达式代码

    正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表 ...

  2. 读取hdfs文件内容

    基础环境: cdh2.71 需要注意: url地址参照 <property> <name>dfs.namenode.servicerpc-address</name> ...

  3. 使用HAProxy、PHP、Redis和MySQL支撑每周10亿请求

    在公司的发展中,保证服务器的可扩展性对于扩大企业的市场需要具有重要作用,因此,这对架构师提出了一定的要求.Octivi联合创始人兼软件架构师Antoni Orfin将向你介绍一个非常简单的架构,使用H ...

  4. asp.net自动打卡、签到程序

    目前公司上下班签到是上局域网的一个系统去点一下,由于打卡比较简单,所以有些快迟到的同事会找已经到公司的人帮忙代打卡.”以其它身份运行程序“来打开IE,去帮人打下,有时多几个人,也要这样操作,我感觉挺麻 ...

  5. 轻松搞定javascript原型链 _proto_

    //如有错误或不同观点,欢迎批评与讨论! 首先,prototype出现的目的,是为了解决 代码重用 的问题 , prototype 相当于是在内存上划分出一个公共的区域, 专用于存放 实例化对象 的相 ...

  6. cURL模拟POST方式提交数据

    curl_post.php文件: 1 $url = 'http://localhost/test/curl_post_deal.php'; 2 3 $post_data = array( 4 'use ...

  7. Thinkphp3.2使用scws中文分词 提取关键词

    SCWS 是 Simple Chinese Word Segmentation 的首字母缩写(即:简易中文分词系统).1.下载scws官方提供的类(这里使用的是pscws第四版的)http://www ...

  8. LinuxCmd

    Q1.关掉字符界面下的屏保 A:setterm -blank 0 Q2.top Top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. ...

  9. 写给初学者css优先级问题

    首先需要搞清楚几个基本概念 1.内嵌样式: 写在元素标签内的例如:<div style="background-color:red"> </div> 2.内 ...

  10. Array 的五种迭代方法 -----every() /filter() /forEach() /map() /some()

    ES5定义了五个迭代方法,每个方法都接收两个参数:要在每一项上运行的函数和运行该函数的作用域对象(可选的),作用域对象将影响this的值.传入这些方法中的函数会接收三个参数:数组的项的值.该项在数组中 ...