确认在 "sqlite3.c" 中,宏 sqlITE_THREADSAFE = 1 或者 2
# define sqlITE_THREADSAFE 1 /* IMP: R-07272-22309 */
#include "Thread.h" extern "C" { #include "sqlite3.h" }; #include <stdio.h> #include "Utility.h" ////////////////////////////////////////////////////////////////////////// int Print(void *pParam,int argc,char ** argv,char ** szColName) { for (int i=0; i<argc; ++i) { printf("%s - %s,",szColName[i],argv[i]); } printf("\n"); return 0; } ////////////////////////////////////////////////////////////////////////// class Demosqlite { typedef ThreadT<Demosqlite> CThread; public: Demosqlite(sqlite3 *psqlite3) : m_psqlite3(psqlite3) { m_lThreadCount = 0; m_threadRead.owner(this); m_threadWrite.owner(this); } VOID Start() { m_threadRead.Start(1); m_threadWrite.Start(4); } VOID Stop() { m_threadRead.Stop(); m_threadWrite.Stop(); } VOID Svc(CThread *pThread,HANDLE hExit) { char *errmsg= NULL; int result = 0; if (pThread == &m_threadRead) { CHAR szsql[128] = { 0 }; while (TRUE) { if (WaitForSingleObject(hExit,1000) == WAIT_OBJECT_0) { break; } wsprintfA(szsql,"SELECT * FROM DemoTable WHERE Thread = 1"); result = sqlite3_exec(m_psqlite3,szsql,Print,NULL,&errmsg); printf("\n\n\n"); wsprintfA(szsql,"DELETE FROM DemoTable WHERE Thread = 1"); result = sqlite3_exec(m_psqlite3,&errmsg); } } if (pThread == &m_threadWrite) { LONG lThread= InterlockedIncrement(&m_lThreadCount); LONG lCount = 0; CHAR szsql[128] = { 0 }; while (TRUE) { if (WaitForSingleObject(hExit,1) == WAIT_OBJECT_0) { break; } wsprintfA(szsql,"INSERT INTO DemoTable VALUES(%d,%d)",lThread,lCount++); result = sqlite3_exec(m_psqlite3,&errmsg); } } } private: sqlite3 *m_psqlite3; LONG m_lThreadCount; CThread m_threadRead; CThread m_threadWrite; }; ////////////////////////////////////////////////////////////////////////// int main() { sqlite3_config(sqlITE_CONFIG_SERIALIZED); char *errmsg = NULL; sqlite3 *psqlite= NULL; int result = OpenDatabase<0>(".\\Demosqlite.db",psqlite); if (result != sqlITE_OK) { return -1; } result = sqlite3_exec(psqlite,"CREATE TABLE IF NOT EXISTS DemoTable(Thread INTEGER,Count INTEGER)",&errmsg); result = sqlite3_exec(psqlite,"DELETE FROM DemoTable",&errmsg); Demosqlite demosqlite(psqlite); demosqlite.Start(); Sleep(1000 * 5); demosqlite.Stop(); sqlite3_close(psqlite); return 0; }原文链接:/sqlite/201866.html