sqlite3编译与查询
1、sqlite3
下载
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编译与查询的更多相关文章
- sqlite3编译
1.sqlite3编译: 1.PC版: 1.解压: tar xvf sqlite-autoconf-3140100.tar.gz cd sqlite-autoconf-3140100/ 2.检查配置 ...
- 关于SQLite3 编译及交叉编译的一些问题
from : http://blog.sina.com.cn/s/blog_5f2e119b0101ibwn.html SQLite3 (http://www.sqlite.org)是一个非常强大的小 ...
- PHP7预编译mysqli查询操作
//连接数据库 $mysqli = new mysqli("localhost", "root", "root", "mobile ...
- Entity Framework 6 Recipes 2nd Edition(13-6)译 -> 自动编译的LINQ查询
问题 你想为多次用到的查询提高性能,而且你不想添加额外的编码或配置. 解决方案 假设你有如Figure 13-8 所示的模型 Figure 13-8. A model with an Associat ...
- [译]SQL Server 之 查询计划缓存和重编译
查询优化是一个复杂而且耗时的操作,所以SQL Server需要重用现有的查询计划.查询计划的缓存和重用在多数情况下是有益的的,但是在某些特殊的情况下,重编译一个查询计划可能能够改善性能. SELECT ...
- EF Core 使用编译查询提高性能
今天,我将向您展示这些EF Core中一个很酷的功能,通过使用显式编译的查询,提高查询性能. 不过在介绍具体内容之前,需要说明一点,EF Core已经对表达式的编译使用了缓存:当您的代码需要重用以前执 ...
- EntityFramework Core 2.0 Explicitly Compiled Query(显式编译查询)
前言 EntityFramework Core 2.0引入了显式编译查询,在查询数据时预先编译好LINQ查询便于在请求数据时能够立即响应.显式编译查询提供了高可用场景,通过使用显式编译的查询可以提高查 ...
- Ubuntu下sqlite3的安装及使用
Sqlite是一款轻型的数据库,实现了多数SQL-92标准,包括事务(原子性,一致性,隔离性和持久性 ACID),触发器与多数复杂查询.对于一个移动手持设备的应用开发者,Sqlite是居家旅行必备数据 ...
- SQLite3 安装、基本操作
1. 安装SQLite3 sudo apt-get install sqlite3 2. 安装Sqlite3编译需要的工具包 如果,你需要的话可以安装该工具包.只是为了体验一把,可以不安装.该项是可选 ...
随机推荐
- [Linked List]Remove Duplicates from Sorted List
Total Accepted: 90247 Total Submissions: 254602 Difficulty: Easy Given a sorted linked list, delete ...
- Ubuntu 12.04 下安装git
---恢复内容开始--- 1.安装build-essential. 列出Git相关包(git-core 和 git-doc)所以来的各个安装包并安装: sudo apt-get build-dep g ...
- title:EL表达式获取Map里面的数值失败的问题
在控制器中定义了一个Map<Integer,String>集合,看似没有问题,将这个集合的对象map传递到一个JSP页面中,我们都知道,用EL表达式 ${map[key]}就可以取得key ...
- Linux03--文件打包与解压
参考了<鸟哥的Linux私房菜> 1.压缩命令 gzip(压缩)与zcat(解压并读出来) gzip 可以说是应用度最广的压缩命令了!目前 gzip 可以解开 compress, zip ...
- 本地拦截genymotion或者Android模拟器的网络请求
我们在主机上面运行了Burp或者fiddler,那么代理已经监听在本机的8080端口了. 那么我们需要在模拟器中进行如下设置: 1.在设置中,长按当前连接的wifi网络,弹出如下: 2. 点击修改网络 ...
- elasticsearch 索引 类型 id
zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat logstash_indexer01.conf input { redi ...
- Memcached 使用与简单监测
Introduce Memcached作为一个高并发内存Cached系统被很多大公司使用,最近也一直在用Memcached做项目也积累了一些相关经验. 本篇博文主要记录Memcached的一些基本使用 ...
- SWOT自我分析
个人信息: 大三学生 二本大学 软件工程专业 一:SWOT自我分析 Strenghs(优势): 1.有着良好的作息习惯,坚持锻炼 2.专注力强,能沉下心来学习 3.有着强烈的危机意思,明白不仅则退的道 ...
- HDU 5700 区间交(线段树)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5700 [题目大意] 给出一个长度为n的数列和m个区间,现在求k个区间,使得他们的区间交内的数列项和 ...
- 打开网页自动弹出QQ临时会话 (打开网站弹出QQ聊天) qq.js文件代
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35 ...