C语言SQLite3事务和锁的操作实例
本文实例讲述了C语言SQLite3事务和锁的操作。分享给大家供大家参考,具体如下:
#include <stdio.h> #include <sqlite3.h> static int lib_get_value_callback(void *buf, int argc, char *argv[], char *column_name[]) { printf("argc:%d,%s argv[0]:%s,%s argv[1]:%s\n",argc,column_name[0],argv[0],column_name[1],argv[1]); return 0; } /*! * \fn war_snprintf. * \brief the functions snprintf produce output according to a format. * \param str: the output buffer. * \param size: size allow writting to buffer * \param format: format string * \param ap: variable list * \return success: size written to buffer ; fail or output longer than size: -1 */ int war_vsnprintf(char *str, size_t size, const char *format, va_list ap) { int res; res = vsnprintf(str, size, format, ap); if (res >= size) { //tr_log(LOG_ERROR, "[v]snprintf failed or output longer than buffer!"); printf("[v]snprintf failed or output longer than buffer!\n"); return -1; } else return res; } /*! * \fn war_snprintf. * \brief the functions snprintf produce output according to a format. * \param str: the output buffer. * \param size: size allow writting to buffer * \param format: format string * \return success: size written to buffer ; fail: -1 */ int war_snprintf(char *str, size_t size, const char *format, ...) { int res; va_list ap; va_start(ap, format); res = war_vsnprintf(str, size, format, ap); va_end(ap); return res; } #define CHECK_RC(rc,szInfo,szErrMsg,db) if(rc!=SQLITE_OK)\ {printf("%s error!\n",szInfo);\ printf("%s\n",szErrMsg);\ sqlite3_free(szErrMsg);\ sqlite3_close(db);\ return 0;} int main(int argc, char * argv[]) { sqlite3 *db; char *dbPath="tr.db"; char *szErrMsg = 0; int rc = 0; rc= sqlite3_open(dbPath, &db); if(rc == SQLITE_OK){ printf("open succeed\n"); } else{ printf("open fail\n"); } //char *szSql ="SELECT type, value FROM tr WHERE id=3"; //char *szSql = "insert into tr values(null,-1,'','false','false',0,'',1,0,'','node','')"; char szSql[128]; int i = 1; //char *Sql = "CREATE TABLE tr(id INTEGER PRIMARY KEY,pid INTEGER DEFAULT -1,name TEXT NOT NULL COLLATE NOCASE, rw varchar(5) DEFAULT 'true',getc varchar(5) DEFAULT 'false',noc INTEGER DEFAULT 0,nocc charchar(3) DEFAULT '',nin INTEGER DEFAULT 1,il INTEGER DEFAULT 0,acl varchar(255) DEFAULT '',type varchar(16) DEFAULT 'node',value TEXT DEFAULT NULL,UNIQUE(pid,name));"; //rc=sqlite3_exec(db,Sql,0,0,&szErrMsg); //CHECK_RC(rc,"create table",szErrMsg,db); for(i = 1;i <= 30;i++) { /*begin*/ rc = sqlite3_exec(db,"BEGIN EXCLUSIVE", NULL,NULL, &szErrMsg); CHECK_RC(rc,"begin",szErrMsg,db); war_snprintf(szSql, sizeof(szSql), "insert into tr values(null,%d,'jjhh','true','false',0,'',1,0,'','string','')", i); rc = sqlite3_exec(db,szSql, lib_get_value_callback, 0, &szErrMsg); CHECK_RC(rc,"insert values",szErrMsg,db); if((rc == SQLITE_OK)&&((i%2)==0)) { rc=sqlite3_exec(db,"COMMIT", NULL,NULL, &szErrMsg); CHECK_RC(rc,"insert values",szErrMsg,db); war_snprintf(szSql, sizeof(szSql), "SELECT id, value FROM tr WHERE pid=%d and name='%s'", i,"jjhh"); rc = sqlite3_exec(db,szSql, lib_get_value_callback, 0, &szErrMsg); CHECK_RC(rc,"query values",szErrMsg,db); } else { rc=sqlite3_exec(db,"ROLLBACK", NULL,NULL, &szErrMsg); CHECK_RC(rc,"insert values",szErrMsg,db); } sleep(5); } sqlite3_close(db); printf("happy everyday!"); return 0; }
希望本文所述对大家C语言程序设计有所帮助。
赞 (0)