1. Enable SQLite to support multi-threaded (not sure whether it is not, add, for the future)
Thread mode can be selected at compile time/start/Run time, reference: http://www.cnblogs.com/liaj/p/4015219.html
My changes:
1) Add compilation options:
-dsqlite_threadsafe=2
2) Open the database file using SQLITE3_OPEN_V2 instead of Sqlite3_open
SQLITE3_OPEN_V2 (Strdbname,sqlite_p, sqlite_open_readwrite| Sqlite_open_create | Sqlite_open_nomutex, NULL);
2. Use Sqlite3_busy_handler to process the Sqlite_busy status (required)
Reference:
Https://www.sqlite.org/c3ref/busy_handler.html
http://iihero.iteye.com/blog/1222539
http://blog.csdn.net/guofu8241260/article/details/36378291
My changes:
Static intCallback_in_busy (void*ptr,intcount) { intTimeout = * ((int*) PTR); Usleep (timeout); return 1;}Static voidSqlgetlock (Sqlite3 *sqlite_p,intms) { if(MS >0) {Sqlite3_busy_handler (sqlite_p, Callback_in_busy, (void*) &ms); } Else{Sqlite3_busy_handler (sqlite_p,0,0); }}intSqlExec (Sqlite3 *sqlite_p,Const Char*strSQL) { Char*perrmsg = NULL;intrc =SQLITE_OK; intRET =-1; Sqlgetlock (Sqlite_p, $); RC= Sqlite3_exec (sqlite_p, strSQL, NULL, NULL, &perrmsg); if(RC! =SQLITE_OK) {printf ("%s%d sqlite3_exec error:%s, strSQL = [%s].\n", __func__, __line__, Sqlite3_errmsg (sqlite_p), strSQL); if(Perrmsg! =NULL) {printf ("%s%d sqlite3_exec error:%s\n", __func__, __line__, perrmsg); Sqlite3_free (PERRMSG); } ret= -1; } Else{ret=0; } returnret;}
SQLite: Multi-threaded operations database "locked" workaround