您的位置:首页 > 数据库

sqlite:多线程操作数据库“database is locked”解决方法

2014-10-14 19:59 1386 查看
1. 使sqlite支持多线程(不确定是否非加不可,暂且加上,以备后患)

可以在编译时/启动时/运行时选择线程模式,参考:/article/5411459.html

我的修改:

1)添加编译选项:

-DSQLITE_THREADSAFE=2


2)打开数据库文件使用sqlite3_open_v2替代sqlite3_open

sqlite3_open_v2(strDbName,sqlite_p, SQLITE_OPEN_READWRITE| SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX, NULL);


2. 使用sqlite3_busy_handler对SQLITE_BUSY状态进行处理(必须)

参考:

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

/article/4354506.html

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

我的修改:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: