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编译需要的工具包 如果,你需要的话可以安装该工具包.只是为了体验一把,可以不安装.该项是可选 ...
随机推荐
- Mschart应用之曲线图表spline
本文主要是Mschart应用之曲线图表spline,实现6个模拟数据的图表,其中数据源X轴为当前系统时间,Y轴是由随机函数产生的不同范围的随机数. 首先是自定义一个数据表,然后产生的数据添加到该数据表 ...
- [IOS]图标尺寸
最新参考网址:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Ico ...
- C#.net 摄像头驱动程序,用avicap32.dll
装了摄像头后一般会有 avicap32.dll文件,没有一样可以用这些代码. 不需要在解决方案里面引用这个.dll文件. 下面有二种写法的例子: 例一: using System;using Syst ...
- C# 输入输出 常量变量
//输出 Console.WriteLine("这是一行文字"); 自动回车的. Console.Write("Hello world"); 不带 ...
- C++进程间通信(常用理解例子)-买票
#include "stdafx.h" #include <iostream>using namespace std; #include "windows.h ...
- 使用ASP.NET MVC+Entity Framework快速搭建博客系统
学习 ASP.NET MVC 也有一段时间了,打算弄个小程序练练手,做为学习过程中的记录和分享. 首先,得确定需求,木有需求的话,那还搞个毛线呀!嗯……大致思考了一下,终于得出如下需求: 1.能自定义 ...
- Hibernate 命名查询NamedQuery (转)
转自:http://blog.163.com/ksm19870304@126/blog/static/374552332011993942391/ 配置方式: static List namedQue ...
- 成都UBER优步司机第六组奖励政策
保底时段详解 滴滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs. ...
- Redis事务和分布式锁
Redis事务 Redis中的事务(transaction)是一组命令的集合.事务同命令一样都是Redis最小的执行单位,一个事务中的命令要么都执行,要么都不执行.Redis事务的实现需要用到 MUL ...
- iOS 开发常用设置
1. iOS设置app应用程序文件共享 设置流程 xcode 打开项目----在 info.plist 文件,添加 UIFileSharingEnabled 并设置属性为 YES, 在app内部,将您 ...