Sqlite: multi-threaded database operation "database is locked" solution, sqlitelocked

Source: Internet
Author: User

Sqlite: multi-threaded database operation "database is locked" solution, sqlitelocked

1. Make sqlite support multithreading (you are not sure whether it is not necessary to add it temporarily for future use)

You can select the thread mode at compile/start/run, refer to: http://www.cnblogs.com/liaj/p/4015219.html

My modifications:

1) Add compilation options:

-DSQLITE_THREADSAFE=2

2) Open the database file and use sqlite3_open_v2 to replace 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)

Refer:

Https://www.sqlite.org/c3ref/busy_handler.html

Http://iihero.iteye.com/blog/1222539

Http://blog.csdn.net/guofu8241260/article/details/36378291

My modifications:

static int callback_in_busy(void *ptr, int count){    int timeout = *((int *)ptr);        usleep(timeout);    return 1;}static void SqlGetLock(sqlite3 *sqlite_p, int ms){    if (ms > 0)    {        sqlite3_busy_handler(sqlite_p, callback_in_busy, (void*)&ms);    }    else    {        sqlite3_busy_handler(sqlite_p, 0, 0);    }}int SqlExec(sqlite3 *sqlite_p, const char *strSql){    char *pErrMsg = NULL;    int rc = SQLITE_OK;    int ret = -1;        SqlGetLock(sqlite_p, 200);        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;    }    return ret;}

 


How does SQLite solve database is locked?

Sqlite is a file. A process can be opened and written, but other processes cannot be written. Arrange your multi-process properly.

Hello, database is locked when I insert data to the sqlite database through the input content in the interface when I am working on a project! Ask you

The database process is not closed and released in time after it is opened ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.