int sqlite3_exec(sqlite3*,const char *sql,sqlite_callback,void *,char **errmsg);
返回值: int 成功SQLITE_OK
参数:
sqlite3* 数据库句柄
const char *sql 要执行的sql语句
sqlite_callback 回调函数
void * 给回调函数的参数
char **errmsg 存储错误---要自己通过sqlite3_free(errmsg);
回调函数--用户数据库查询
int (*sqlite3_callback)(void*, int, char**, char**);
int LoadMyInfo( void * para, int n_column, char ** column_value, char ** column_name )
3.关闭数据库文件
int sqlite3_close(sqlite3*);
返回值: int 成功SQLITE_OK
参数:
sqlite3* 数据库句柄
例子:
#include <stdio.h>
#include "sqlite3.h"
int main()
{
//1.打开数据库
sqlite3 *ppdb = NULL;
int ret = sqlite3_open("myapi.db", &ppdb);
if(ret != SQLITE_OK)
{
perror("open fail");
return -1;
}
//2.执行sql语句
//创建表格 create table apitest (id int, name text);
//插入数据 insert into apitest values(0,‘aaa‘);
const char *create_sql = "create table apitest (id int, name text)";
char *errmsg = NULL;
ret = sqlite3_exec(ppdb, create_sql, NULL, NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("%s\n",errmsg);
}
sqlite3_free(errmsg);
const char* insert_sql = "insert into apitest values(0,‘aaa‘);";
ret = sqlite3_exec(ppdb, insert_sql, NULL, NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("%s\n",errmsg);
}
sqlite3_free(errmsg);
//3.关闭数据库
ret = sqlite3_close(ppdb);
return 0;
}
回调函数查询操作
//查询
const char *select_sql = "select * from apitest;";
ret = sqlite3_exec(ppdb, select_sql, callback, NULL, &errmsg);
if(ret != SQLITE_OK)
{
printf("查询--%s\n",errmsg);
}
sqlite3_free(errmsg);
//回调函数是根据查询的数据---有多少行就被调用多少次,返回0继续查询直到全部查询完,如果是非0就终止查询
int callback(void *arg, int col, char **values, char **names)
{
printf("回调--%d\n",col);
return 0;
}
非回调函数查询
int sqlite3_get_table(sqlite3*,const char *sql,char ***resultp,int *nrow,int *ncolumn,char **errmsg);
返回值:SQLITE_OK成功
参数:
sqlite3* 数据库句柄
const char *sql sql查询语句
char ***resultp 查询的到的数据表
int *nrow 查到数据表的行
int *ncolumn 查到的数据表的列
char **errmsg 存储错误
sqlite3_free_table(resultp);//使用后要释放
sqlite3_free(errmsg)
例子代码:
#include <stdio.h>
#include <sqlite3.h>
int main()
{
//1打开
sqlite3 *ppdb = NULL;
int ret = sqlite3_open(":memory:", &ppdb);//打开数据库-数据存储在内存中
//int ret = sqlite3_open("my.db", &ppdb);
if(ret != SQLITE_OK)
{
printf("open fail");
goto error;
}
//2创建数据表格
char *errmsg = NULL;
ret = sqlite3_exec(ppdb, "create table if not exists test(id int , number int)",
NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
printf("%s", errmsg);
goto error;
}
//3插入数据
char insert_sql[128]={0};
for(int i=0; i<10; i++)
{
sprintf(insert_sql, "insert into test values(%d, %d)", i, i*100);
ret = sqlite3_exec(ppdb, insert_sql, NULL, NULL, &errmsg);
if(ret != SQLITE_OK)
{
printf("%s", errmsg);
goto error;
}
}
//4.查询数据--非回调
char **resultp = NULL;
int row, col;
ret = sqlite3_get_table(ppdb, "select * from test", &resultp, &row, &col, &errmsg);
printf("%d:%d\n", row, col);
//printf("%s\t %s\n", resultp[0], resultp[1]);
for(int i=0 ; i<(row+1); i++)
{
for (int j=0; j<col; j++) {
printf("%s\t", resultp[i*col+j]);
}
printf("\n");
}
//释放表格控件
sqlite3_free_table(resultp);
sqlite3_free(errmsg);
error:
sqlite3_free(errmsg);
sqlite3_close(ppdb);
return -1;
}