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. 06JS高级创建对象使用原型共享对象方法

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. Servlet 浅谈(一)

    Servlet 的生命周期 类加载.实例化,init方法,service方法,destroy方法.关于这点,后面会有详细介绍. 什么是容器? 因为Servlet没有main方法,所以它受控于另一个Ja ...

  3. sulime运行 java 和 php

    执行php脚本 1. 先配置好php环境变量 2. Tools -> Build System -> New Build System.... {      "cmd" ...

  4. Android Intent的花样启动

    刚开始看郭大神的<>,实现以下里面的一些例子.Intent的花样启动 显示Intent的使用. 实例化一个Intent,并且制定当前的activity和要跳转到的activity Inte ...

  5. Android_实现静默安装和卸载应用

    转:http://www.cnblogs.com/ondream/archive/2012/04/13/2446138.html 前段时间做了一个批量安装卸载应用程序的小应用,由于安装卸载应用程序的部 ...

  6. Transfer learning across two sentiment classes using deep learning

    用深度学习的跨情感分类的迁移学习 情感分析主要用于预测人们在自然语言中表达的思想和情感. 摘要部分:two types of sentiment:sentiment polarity and poli ...

  7. java的几个版本以及jre,jdk等概念——【转载】JDK、Java SE、Java EE、Java ME我该选

    我们平时使用的一些软件,有一部分需要Java环境的支持,但是SUN那么多的产品,让人眼花缭乱的版本号,前看后看都差不多的缩写,让我们选择起来的时候常常望而却步,只好跟着感觉走.所以下面我要介绍的就是那 ...

  8. 适合入门自学服装裁剪滴书(更新ing)

    [♣]适合入门自学服装裁剪滴书(更新ing) [♣]适合入门自学服装裁剪滴书(更新ing) 适合入门自学服装裁剪滴书(更新ing) 来自: 裁缝阿普(不为良匠,便为良医.) 2014-04-06 23 ...

  9. linux学习之十、变量的取用与配置:echo, 变量配置守则, unset

    vim vim 的环境设定参数 :set nu :set nonu 就是设定与取消行号啊! :set hlsearch :set nohlsearch hlsearch 就是 high light s ...

  10. 获取枚举Name,Value,Description两种方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...