服务器之家

服务器之家 > 正文

C语言SQLite3事务和锁的操作实例

时间:2021-05-25 14:01     来源/作者:lifan5

本文实例讲述了C语言SQLite3事务和锁的操作。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#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语言程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部