sqlite 基本回调用法

前端之家收集整理的这篇文章主要介绍了sqlite 基本回调用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>

	//n_column 是列数,在select语句中采用了*,而该表有两列,所以此值为2,column_value 和 column_name 应该看成是一维指针,它指向的是一个
	//char *的字符串
int LoadMyInfo (void *para,int n_column,char ** column_value,char ** column_name){
	int i;
	printf("record %d field \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;
}

int main(){
	sqlite3 *db;
	int result;
	char *errmsg = NULL ;
	result = sqlite3_open("C://sely.db",&db);
	if( result != sqlITE_OK){
		return -1;
	}
	
	result = sqlite3_exec(db,"create table MyTable_1(ID integer primary key autoincrement,name varchar(32))",NULL,&errmsg);
    if(result != sqlITE_OK){
		printf("create table error: %d,reason : %s",result,errmsg);
	}

	result = sqlite3_exec(db,"insert into MyTable_1 (name)  values(\"walk\")",&errmsg);
	if(result != sqlITE_OK){
		printf("create table error: %d,errmsg);
	}

    result = sqlite3_exec(db,"insert into MyTable_1 (name) values(\"bus\")","insert into MyTable_1 (name)  values(\"bike\")","insert into MyTable_1 (name) values(\"car\")",errmsg);
        }
	
	//loadMyInfo 是回调函数,每次有一个记录被查询出,都会调用一次,结果集中有多少记录,就会执行多少次!    
	result = sqlite3_exec(db,"select *from MyTable_1",LoadMyInfo,errmsg);
	}

	sqlite3_close(db);
	system("pause");
	return 0;
}
/* 执行结果如下:
record 2 field
field name : ID field value: 1
field name : name field value: walk
-------------------------
record 2 field
field name : ID field value: 2
field name : name field value: bus
-------------------------
record 2 field
field name : ID field value: 3
field name : name field value: bike
-------------------------
record 2 field
field name : ID field value: 4
field name : name field value: car
-------------------------
Press any key to continue . . .
*/

猜你在找的Sqlite相关文章