天天看点

sqlite c语言api 实例,sqlite3数据库c语言常用接口应用实例



#include

#include

#include

static int select_callback(void *data,int argc,char **argv,char **azColName){

int i;

printf("%s",(char*)data);

for(i=0; i < argc; i++){

printf("%s = %s\n",azColName[i],argv[i] ? argv[i] : "NULL");

}

printf("\n");

return 0;

}

int main(int argc,char* argv[])

{

sqlite3 *db;

char *zErrMsg = 0;

int rc;

rc = sqlite3_open("test.db",&db);

if( rc ){

fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));

exit(0);

}else{

fprintf(stderr,"Opened database successfully\n");

}

char* sql;

sql = "create table healthinfo (" \

"sid int primary key not null," \

"name text not null," \

"ishealth char(4) not null);";

rc = sqlite3_exec(db,sql,NULL,&zErrMsg);

if( rc != sqlITE_OK ){

fprintf(stderr,"sql error: %s\n",zErrMsg);

sqlite3_free(zErrMsg);

}else{

fprintf(stdout,"Table created successfully\n");

}

sql = "insert into healthinfo (sid,name,ishealth)" \

"values (201601001,'xiaowang','yes');" \

"insert into healthinfo (sid,ishealth)" \

"values (201601002,'xiaoli',ishealth)" \

"values (201601003,'xiaozhang','no');" \

"insert into healthinfo (sid,ishealth)" \

"values (201601004,'xiaozhou',ishealth)" \

"values (201601005,'xiaosun','yes');";

rc = sqlite3_exec(db,"Table insert data successfully\n");

}

char* strname = "xiaoyang";

//char strname[256] = {'x','i','a','o','y','n','g'};

char sql2[256] = {'0'};

sprintf(sql2,"insert into healthinfo (sid,ishealth) values (201601006,'%s','yes');",strname);

rc = sqlite3_exec(db,sql2,"Table insert data successfully\n");

}

sql = "insert into healthinfo (sid,ishealth)" \

"values (:sid,:name,:ishealth);";

sqlite3_stmt *stmt;

sqlite3_prepare_v2(db,strlen(sql),&stmt,NULL);

printf("max_parameter_count = %d\n",sqlite3_bind_parameter_count(stmt));

printf("sid parameter index = %d\n",sqlite3_bind_parameter_index(stmt,":sid"));

printf("name parameter index = %d\n",":name"));

printf("ishealth parameter index = %d\n",":ishealth"));

printf("index = 1 's parameter's name = %s\n",sqlite3_bind_parameter_name(stmt,1));

sqlite3_bind_int(stmt,1,201601007);

sqlite3_bind_text(stmt,2,"xiaoqian",-1,NULL);

sqlite3_bind_text(stmt,3,"yes",NULL);

//sqlite3_bind_blob(stmt,sectionData,4096,sqlITE_STATIC);

rc = sqlite3_step(stmt);

printf("step() return %s\n",rc == sqlITE_DONE ? "sqlITE_DONE" \

: rc == sqlITE_ROW ? "sqlITE_ROW" : "sqlITE_ERROR");

sqlite3_reset(stmt);

sqlite3_bind_int(stmt,201601008);

sqlite3_bind_text(stmt,"xiaowu",NULL);

sqlite3_bind_text(stmt,NULL);

sqlite3_step(stmt);

sqlite3_finalize(stmt);

//sql = "select * from healthinfo;";

sql = "select * from healthinfo limit 4 offset 2;";

sqlite3_prepare_v2(db,NULL);

printf("total_column = %d\n",sqlite3_column_count(stmt));

while(sqlite3_step(stmt) == sqlITE_ROW){

int len_sid = sqlite3_column_bytes(stmt,0);

int len_name = sqlite3_column_bytes(stmt,1);

int len_ishealth = sqlite3_column_bytes(stmt,2);

printf("sid = %d,len = %d\n",sqlite3_column_int(stmt,0),len_sid);

printf("name = %s,sqlite3_column_text(stmt,1),len_name);

printf("ishealth = %s,2),len_ishealth);

//unsigned char* srcdata = sqlite3_column_blob(stmt,0);

}

printf("\n");

sqlite3_finalize(stmt);

const char* data = "select call back function call!\n";

sql = "select * from healthinfo where ishealth == 'yes';";

rc = sqlite3_exec(db,select_callback,data,"Table select successfully\n");

}

data = "update call back function call!\n";

sql = "update healthinfo set ishealth = 'no' where name='xiaoli';" \

"select * from healthinfo where ishealth == 'yes';";

rc = sqlite3_exec(db,"Table update successfully\n");

}

sql = "drop table healthinfo;";

rc = sqlite3_exec(db,"Table droped successfully\n");

}

char sql5[256];

char* tname = "abc";

sprintf(sql5,"create table if not exists %s ("\

"id int not null," \

"name text not null);",tname);

printf("%s\n",sql5);

rc = sqlite3_exec(db,sql5,"Table created successfully\n");

}

sqlite3_close(db);

} 学了一天的sqlite3编程,学习用例贴出来,基本的函数都用到了,也进行了详细的说明,希望对看的人有帮助。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

小编个人微信号 jb51ccc

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!