[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]$