sqlite3.7.17 programming C callback function through sqlite3_exec

[lake@lake-aliyun-sh-china sqlite3]$ cat   sqlite3-search.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sqlite3.h>

#define DB_FILE_NAME  "stu.db"

/**
 *
 *
 *  sqlite3  stu.db
 *  SQLite version 3.7.17 2013-05-20 00:56:22
 *  Enter ".help" for instructions
 *  Enter SQL statements terminated with a ";"
 *  sqlite> .schema
 *  CREATE TABLE stu(id Integer PRIMARY KEY, name char,score Integer);
 *  sqlite> .table
 *  stu
 *  sqlite> select * from stu;
 *  1|lisi|90
 *  2|zhangsan|80
 *  5|five|80
 *  33|three|80
 *  34|four|80
 *  sqlite>
 *
 *
 * */


int main()
{
  char * errmsg = NULL ;
  sqlite3 * db = NULL;
  int result;
  result = sqlite3_open(DB_FILE_NAME, &db );

  if( result != SQLITE_OK )
  {
        return 0;
 }

  printf("sqlite open OK \n");

   int my_callbackfun(void * para, int n_column, char ** column_value, char ** column_name);
  //search by sql
  result = sqlite3_exec(db, "select * from stu", my_callbackfun, NULL, &errmsg);

  if (result != SQLITE_OK)
  {
        printf("sql execute error = %d,error message: %s \n", result, errmsg);
  }
 sqlite3_close( db );

  return 1;
}


int my_callbackfun(void * para, int n_column, char ** column_value, char ** column_name)
{
        int i;
         printf("call back  fun  \n");
        printf("Number of fields :  %d \n", n_column);

        for (i = 0; i < n_column; i++)
        {
                printf("field name :%s , field value :%s \n", column_name[i], column_value[i]);
        }
        printf("------------------ \n") ;
        return 0  ;  // If an sqlite3_exec() callback returns non-zero, the sqlite3_exec() routine returns SQLITE_ABORT

}
[lake@lake-aliyun-sh-china sqlite3]$
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值