sqlite3 解决并发读写冲突的问题

  1 #include "stdafx.h"
  2 #include "sqlite3.h"
  3 #include <iostream>
  4 #include <process.h>
  5 
  6 //sqllite 相关变量
  7 sqlite3 *sql_db;
  8 char *sql_ErrMsg;
  9 int sql_rc;
 10 sqlite3_stmt *sql_statement;
 11 
 12 //写入重试客克服锁库的问题
 13 void sql_insert_proc(char insert_state[400])
 14 {
 15     while (1)
 16     {
 17         if (SQLITE_OK != sqlite3_exec(sql_db, insert_state, 0, 0, &sql_ErrMsg))
 18         {
 19             if (strstr(sql_ErrMsg, "database is locked"))
 20             {
 21                 printf("try again!\n");
 22                 continue;
 23             }
 24             else
 25             {
 26                 printf("other error! \n");
 27                 break;
 28             }
 29         }
 30         else
 31         {
 32             printf("ok insert !\n");
 33             break;
 34         }
 35     }
 36 }
 37 
 38 
 39 int _tmain(int argc, _TCHAR* argv[])
 40 {
 41     sql_rc = sqlite3_open("C:\\SQLite\\test.db", &sql_db);
 42     if (sql_rc){
 43         fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(sql_db));
 44         sqlite3_close(sql_db);
 45         return(1);
 46     }
 47     printf("hello world!\n");
 48     getchar();
 49     int i;
 50 
 51     
 52     for (i = 0; i < 10000; i++)
 53     {
 54         int step_result = SQLITE_BUSY;
 55         char select_statement[400] = "";
 56         char selectname[25] = "";
 57 
 58         //读取重试克服锁库的问题
 59         //SQLITE_ERROR、SQLITE_BUSY、SQLITE_MISUSE、SQLITE_ROW、SQLITE_DONE
 60         while (step_result == SQLITE_BUSY)
 61         {
 62             memset(select_statement, 0, 400);
 63             memset(selectname, 0, 25);
 64 
 65             sprintf_s(select_statement, "select name from test order by id desc limit 1");
 66 
 67             sql_rc = sqlite3_prepare(sql_db, select_statement, -1, &sql_statement, NULL);
 68 
 69             if (sql_rc != SQLITE_OK)
 70             {
 71                 fprintf(stderr, "prepare error return code = %d\n", sql_rc);
 72             }
 73 
 74             step_result = sqlite3_step(sql_statement);
 75 
 76             while (step_result == SQLITE_ROW)
 77             {
 78                 strcpy_s(selectname, (char *)sqlite3_column_text(sql_statement, 0));
 79                 step_result = sqlite3_step(sql_statement);
 80             }
 81 
 82             sqlite3_finalize(sql_statement);
 83         }
 84         
 85         printf("index = %d : %s\n", i, selectname);
 86     }
 87     
 88     /*
 89     for (i = 0; i < 10000; i++)
 90     {
 91         char insert_statement[400] = "";
 92         sprintf_s(insert_statement, "insert into test (name) values('proc1 hello %d')", i);
 93         printf("%s\n", insert_statement);
 94         sql_insert_proc(insert_statement);
 95     }
 96     */
 97     
 98 
 99     sqlite3_close(sql_db);
100     return 0;
101 }

 

    原文作者:sqlite
    原文地址: https://www.cnblogs.com/hushaojun/p/5635484.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞