资源描述:
《c程序对sqlite数据库的操作》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、在linux上安装sqlite数据库实例代码(test_sqlite.h,test_main.c)test_sqlite.h:#includesqlite3*open_sqlite_db(char*dbname);voidcreate_sqlite_table(sqlite3*db,char*tablename,char*SQL);voidinsert_sqlite_table(sqlite3*db,char*tablename,char*str);voidquery_sqlite_table(sqlite3*db,char*tab
2、lename);voidclose_sqlite_db(sqlite3*db);sqlite3*open_sqlite_db(char*dbname){intrc;sqlite3*db=NULL;rc=sqlite3_open(dbname,&db);returndb;}voidcreate_sqlite_table(sqlite3*db,char*tablename,char*SQL){charsql[1024];char*errmsg;sprintf(sql,"CREATETABLE%s(%s);",tablename,SQL);sqlite3_ex
3、ec(db,sql,0,0,&errmsg);}voidinsert_sqlite_table(sqlite3*db,char*tablename,char*str){charsql[1024];char*errmsg;sprintf(sql,"INSERTINTO%sVALUES(null,%s);",tablename,str);sqlite3_exec(db,sql,0,0,&errmsg);}/*查询数据库db中tablename表中的内容*/voidquery_sqlite_table(sqlite3*db,char*tablename){ch
4、arsql[1024];intnrow=0,ncolumn=0;char*errmsg;char**azResult;inti=0,j=0;sprintf(sql,"SELECT*FROM%s",tablename);sqlite3_get_table(db,sql,&azResult,&nrow,&ncolumn,&errmsg);printf("row:%dcolumn=%d",nrow,ncolumn);for(i=1;i<=nrow;i++){printf("the%drowis",i);for(j=i*ncolumn;j<(i+1)*nco
5、lumn;j++)printf("%s",azResult[j]);printf("");}sqlite3_free_table(azResult);}voidclose_sqlite_db(sqlite3*db){sqlite3_close(db);}test_main.c:#include#include“test_sqlite.h”intmain(void){sqlite3*db=NULL;chardbname[]=”test”;chartablename[]=”20115050”;charstr[]=”Bob”;charcr
6、eate_sql[]=”idINTEGERPRIMARYKEY,nameVARCHAR(20)”;db=open_sqlite_db(dbname);create_sqlite_table(db,tablename,create_sql);insert_sqlite_table(db,tablename,str);query_sqlite_table(db,tablename);close_sqlite_db(db);return0;}编译:gcc–otesttest_main.c–lsqlite3