C语言中怎么利用SQLite3实现事务和锁,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
公司主营业务:成都网站制作、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出峡江免费做网站回馈大家。
具体如下:
#include#include 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实现事务和锁问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。