时间:2021-05-20
本文实例讲述了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++) { 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语言程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Python简单操作sqlite3的方法。分享给大家供大家参考,具体如下:importsqlite3defTest1():#con=sqlite3
本文实例讲述了C#SQLite事务操作方法。分享给大家供大家参考,具体如下:在C#中执行Sqlite数据库事务有两种方式:SQL代码和C#代码1.SQL代码:B
本文实例讲述了C#中增加SQLite事务操作支持与使用方法。分享给大家供大家参考,具体如下:在C#中使用Sqlite增加对transaction支持usingS
其实,在sqlite3中没有top的语法结构,但在sqlite3中有相关的语法能实现跟top语法相同的功能,sqlite3sql是用limit这样的语法来实现的
备注:本文所有操作均在root用户下进行。1、安装sqlite3ubuntu下安装sqlite3直接在终端运行命令:#apt-getinstallsqlite3