linux_sqlite3学习
来源:互联网 发布:存储数据的最小单位 编辑:程序博客网 时间:2024/06/11 20:38
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
int callback(void *arg, int clu_num, char **value, char **name)
{
int i = 0;
for(i; i <= clu_num-1; i++)
{
printf("字段名:%s 值:%s\n", name[i], value[i]);
}
return 0;
}
int main()
{
//1、创建sqlite3 操纵对象
struct sqlite3 *db=NULL;
char *errbuf = (char *)malloc(200);
do
{
//2、打开或创建数据库
int ret = -1;
if( 0 != (ret=sqlite3_open("taoli.db",&db)) )
{
goto err;
}
//3、执行sql命令
if( SQLITE_OK != sqlite3_exec(db, "create table student(id INTEGER PRIMARY KEY, name TEXT);", NULL, NULL, &errbuf) )
{
printf("exec create err info:\n%s\n", errbuf);
goto err;
}
if( SQLITE_OK != sqlite3_exec(db, "insert into student values (1, 'tanziqi');", NULL, NULL, &errbuf) )
{
printf("exec insert err info:\n%s\n", errbuf);
goto err;
}
if( SQLITE_OK != sqlite3_exec(db, "select * from student;", callback, NULL, &errbuf) )
{
printf("exec select err info:\n%s\n", errbuf);
goto err;
}
//4、其它业务逻辑
//5、关闭数据库,清理缓冲区等资源
sqlite3_free(errbuf);
sqlite3_close(db);
}while(0);
return 0;
err:
sqlite3_free(errbuf);
sqlite3_close(db);
printf("failed\n");
exit(1);
}
//==============================================示例二==================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <set>
#include "sqlite3.h"
using namespace std;
std::set<int> g_id;
std::set<char *> g_name;
int main()
{
//1、创建sqlite3 操纵对象及错误信息缓冲区
char *errbuf = NULL;//
struct sqlite3 *db=NULL;
do
{
//2、打开或创建数据库
int ret = -1;
if( 0 != (ret=sqlite3_open("taoli.db",&db)) )
{
goto err;
}
//3、执行sqlite3_get_table //下标0~(pcolumn-1)的是字段名 pcolumn~(prow-1)是对应的第一行结果集
char **pdata;
int prow = 0, pcolumn = 0;
if(SQLITE_OK != sqlite3_get_table(db, "select * from student;", &pdata, &prow, &pcolumn, &errbuf))
{
printf("err info:\n%s\n", errbuf);
goto err;
}
//4、业务逻辑
printf("字段名 [%s] [%s]\n" , pdata[0], pdata[1]);
printf("值:%s %s\n", pdata[2], pdata[3]);
//5、释放结果集资源free_table
sqlite3_free_table(pdata);
//6、关闭数据库(释放句柄与错误缓冲区)
sqlite3_free(errbuf);
sqlite3_close(db);
}while(0);
return 0;
err:
sqlite3_free(errbuf);
sqlite3_close(db);
return -1;
}
- linux_sqlite3学习
- linux_sqlite3添加新信息,插入信息
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- 学习
- generatorConfiguration配置文件及其详细解读
- 【枚举】tyvj 1387 迷你火车头
- Eye-Dome-Lighting (EDL)
- 文章标题
- 识别,基于内容的检索,检测
- linux_sqlite3学习
- Conditionvariable的用法
- 151104 系统坑爹计
- 字符编码问题
- centos 64 python2.6升级到2.7后中文输入法问题
- Unity5 Screen-Space Subsurface Scattering屏幕空间次表面散射SSSSS
- Java中有关Null的9件事
- (一)ActiveMQ学习教程——安装(和topic实例)
- centos中apache配置