sqlite回调函数的解释与使用

>在sqlite3的api函数中有一个sqlite3_exec,用来执行sql语句:

函数原型:
int sqlite3_exec
(
sqlite3* ppDb, /* 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 */
);
第1个参数不再说了,是sqlite3_open函数得到的指针。
第2个参数constchar*sql是一条sql 语句,以\0结尾。
第3个参数sqlite3_callback 是回调,sqlite3每查询到一条结果,都会去调用你提供的这个函数。
第4个参数void*是你所提供的指针,你可以传递任何一个指针参数到这里,这个参数最终会传到回调函数里面,如果不需要传递指针给回调函数,可以填NULL。
第5个参数char** errmsg 是错误信息。注意是指针的指针。sqlite3里面有很多固定的错误信息。执行sqlite3_exec 之后,执行失败时可以查阅这个指针。
回调函数的格式:

int sqlite_callback(
void* pvoid, /* 由 sqlite3_exec() 的第四个参数传递而来 */
int argc, /* 表的列数 */
char** argv, /* 指向查询结果的指针数组, 可以由 sqlite3_column_text() 得到 */
char** col /* 指向表头名的指针数组, 可以由 sqlite3_column_name() 得到 */
);
在所有的回掉函数中,都要加上return 0;否则失去其回调函数的意义。

/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co.,     Ltd. 
File name:
Author:Jerey_Jobs    Version:0.1    Date: 
Description:
Funcion List: 
*****************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>

int create_table(sqlite3 *db)
{
    char *errmsg = NULL;
    char *sql;

    sql = "create table if not exists mytable(id integer primary key,name txt);";

    if(SQLITE_OK != sqlite3_exec(db,sql,NULL,NULL,&errmsg))
    {
        printf("operater failed1 : %s \n",errmsg);
        exit(0);
    }
}

int insert_record(sqlite3 *db)
{
    char *errmsg;
    char sql[100];
    char name[10];
    int id;
    int n;
    int i;
    printf("enter the number \n");
    scanf("%d",&n);
    for(i = 0; i < n;i++)
    {
    printf("enter the id you want to insert :\n");
    scanf("%d",&id);
    printf("enter the name you want to insert :\n");
    scanf("%s",name);

    sprintf(sql,"insert into mytable(id,name) values(%d,'%s');",id,name);
    if(SQLITE_OK != sqlite3_exec(db,sql,NULL,NULL,&errmsg))
    {
        printf("operater failed2 : %s \n",errmsg);
        exit(0);
    }
    }

/*  sql = "insert into mytable(id,name) values(NULL,'xie');";
    if(SQLITE_OK != sqlite3_exec(db,sql,NULL,NULL,&errmsg))
    {
        printf("operater failed3 : %s \n",errmsg);
        exit(0);
    }

    sql = "insert into mytable(id,name) values(NULL,'zhen');";
    if(SQLITE_OK != sqlite3_exec(db,sql,NULL,NULL,&errmsg))
    {
        printf("operater failed4 : %s \n",errmsg);
        exit(0);
    }*/
}

int displaycb(void *para,int n_column,char **column_value,char **column_name)
{
    int i;
    printf("total column is %d \n",n_column);
    for(i = 0;i < n_column;i++)
    {
//      printf("\t\t %s \t\t %s \n",column_name[i],column_value[i]);
            printf("column_name : %s ----> column_value : %s \n",column_name[i],column_value[i]);
    }
    return 0;
}

int inquire_usecb(sqlite3 *db)
{
    char *errmsg;
    char *sql;

    sql = "select * from mytable;";

    if(SQLITE_OK != sqlite3_exec(db,sql,displaycb,NULL,&errmsg))
    {
        printf("operater failed5 : %s \n",errmsg);
        exit(0);
    }
}
int delete_record(sqlite3 *db)
{
    char *errmsg;
    char sql[100];
    int id;

    printf("enter the id you want to delete\n");
    scanf("%d",&id);

    sprintf(sql,"delete from mytable where id = %d",id);

    if(SQLITE_OK != sqlite3_exec(db,sql,NULL,NULL,&errmsg))
    {
        printf("operater failed6 :%s \n",errmsg);
        exit(0);
    }
}

int inquire_nocb(sqlite3 *db)
{
    int nrow,ncolumn;
    char **azresult;
    char *sql;
    char *errmsg;
    int i;
    sql = "select * from mytable;";
    if(SQLITE_OK != sqlite3_get_table(db,sql,&azresult,&nrow,&ncolumn,&errmsg))
    {
        printf("operater failed : %s\n",errmsg);
        exit(0);
    }
    printf("row :%d        column :%d\n",nrow,ncolumn);
    printf("the result of querying : \n");
    for(i = 0;i < (nrow + 1) * ncolumn;i++)
    {
        printf("%10s",azresult[i]);
        if((i + 1) % ncolumn == 0)
        {
            printf("\n");
        }
    }
    sqlite3_free_table(azresult);
}
int main()
{
    sqlite3 *db = NULL;
    int ret;

    ret = sqlite3_open("mydatabase.db",&db);

    if(ret != SQLITE_OK)
    {
        perror("open error!\n");
        exit(0);
    }
    else
    {
        printf("you have opened a qulite3 database successfully !\n");
    }

    create_table(db);
    insert_record(db);
    inquire_usecb(db);
    delete_record(db); 
    inquire_nocb(db);
    sqlite3_close(db);
    return 0;
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
SQLite3 的回调函数是在执行 SQL 语句时,将结果返回给应用程序的一种方式。通过回调函数,我们可以在 SQL 语句执行期间对返回的结果进行处理或者操作。 下面是 SQLite3 回调函数使用方法: 1. 定义回调函数 回调函数一般会在 SQLite3 执行 SQL 语句时被调用,因此我们需要先定义一个回调函数,它必须符合 SQLite3 规定的特定格式。 例如,我们定义一个回调函数,用于将查询结果打印出来: ```c int callback(void *NotUsed, int argc, char **argv, char **azColName) { int i; for(i = 0; i < argc; i++) { printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; } ``` 回调函数的参数说明: - `void *NotUsed`:回调函数可以接收一个指针类型的参数,但它并不会被使用,通常可以将其设置为 `NULL`。 - `int argc`:SQL 语句返回的结果集中有多少列。 - `char **argv`:一个包含结果集中每一列的值的字符串数组。 - `char **azColName`:一个包含结果集中每一列的名称的字符串数组。 2. 执行 SQL 语句 在调用 SQLite3 执行 SQL 语句的函数时,我们需要将回调函数作为其中一个参数传递进去。 例如,我们查询一个名为 `users` 的表中所有的数据,将结果通过回调函数输出: ```c sqlite3 *db; char *zErrMsg = 0; int rc; const char* data = "Callback function called"; char *sql = "SELECT * FROM users"; rc = sqlite3_open("test.db", &db); if (rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); return(0); } else { fprintf(stderr, "Opened database successfully\n"); } rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg); if (rc != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { fprintf(stdout, "Operation done successfully\n"); } sqlite3_close(db); ``` 在执行 SQL 语句时,我们将回调函数 `callback` 作为第三个参数传递给了 `sqlite3_exec` 函数。此外,我们还可以通过第四个参数将自定义数据传递给回调函数,在回调函数使用。 总之,SQLite3 的回调函数可以让我们在执行 SQL 语句时自定义处理结果的方式,非常灵活和方便。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值