[英]How to Lock and then Unlock sqlite database using c?
I am testing for this question using SQLite 3 database in C language. 我正在使用C语言的SQLite 3数据库测试此问题 。
On one thread, I am accessing a table 'audio' and on the other thread I am renaming that table to dummy and back to audio . 在一个线程上,我正在访问一个表'audio',在另一个线程上,我将该表重命名为dummy并返回到audio 。 But I get following errors after sometime
但一段时间后出现以下错误
database is locked from one thread 数据库从一个线程被锁定
cannot start transaction within another transaction from another thread. 无法从另一个线程在另一个事务中启动事务 。
I read somewhere that using transaction locks a database and unlocks it after commit. 我在某处读到,使用事务锁定数据库并在提交后将其解锁。 But that's not happening here.
但这不是在这里发生。 Can anyone suggest the mistake or proper solution.
任何人都可以提出错误或适当的解决方案。
Here is my code : 这是我的代码:
#include <stdio.h>
#include <pthread.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <unistd.h>
static int callback1(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
printf("%s = %s\n", azColName[0], argv[0] ? argv[0] : "NULL");
printf("\n");
return 0;
}
void *dbOps1 (void * name)
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "";
/* Open database */
rc = sqlite3_open("myDB.db", &db);
if( rc ){
printf("Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
printf("Opened database successfully\n");
}
/* Create SQL statement */
sql = "SELECT * from audio";
/* Execute SQL statement */
for(;;)
{
rc = sqlite3_exec(db, sql, callback1, (void*)data, &zErrMsg);
if( rc != SQLITE_OK ){
printf("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
// printf("Operation done successfully\n");
}
}
sqlite3_close(db);
return 0;
}
static int callback2(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
printf("%s = %s\n", azColName[1], argv[1] ? argv[1] : "NULL");
printf("\n");
return 0;
}
void *dbOps2 (void * name)
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "";
/* Open database */
rc = sqlite3_open("myDB.db", &db);
if( rc ){
printf("Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
printf("Opened database successfully\n");
}
/* Create SQL statement */
sql = "Begin transaction;alter table audio rename to dummy;alter table dummy rename to audio;commit transaction;";
/* Execute SQL statement */
for(;;)
{
rc = sqlite3_exec(db, sql, callback2, (void*)data, &zErrMsg);
if( rc != SQLITE_OK ){
printf("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
// printf("Operation done successfully\n");
}
}
sqlite3_close(db);
return 0;
}
int main()
{
pthread_t thread1,thread2;
char * message1="Thread 1";
char * message2="Thread 2";
int iret1=pthread_create(&thread1,NULL,dbOps1,(void *)message1);
int iret2=pthread_create(&thread2,NULL,dbOps2,(void *)message2);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
printf("Thread 1 return code :%d\n",iret1);
printf("Thread 2 return code :%d\n",iret2);
return 0;
}
You get the "database is locked" error because you did not set a busy timeout that is long enough. 您收到“数据库已锁定”错误,因为您没有设置足够长的繁忙超时 。 (The default, 0, certainly isn't.)
(默认值0肯定不是。)
The "cannot start transaction within another transaction" is just a consequence of not committing or rolling back the first transaction before starting the second one. “无法在另一笔交易中开始交易”仅是在开始第二笔交易之前未提交或回退第一笔交易的结果。 (
sqlite3_exec
stops at the first error.) (
sqlite3_exec
在第一个错误处停止。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.