您好,登錄后才能下訂單哦!
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語言中怎么利用SQLite3實現事務和鎖問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。