This problem does not occur when using other interfaces, e.g. the MySQL command line or Navicat or PHP connections. Therefore I assume that you have some locking logic in your C program which is responsible, not MySQL. Do you use 3rd party libraries which might contain the locking? What is the MySQL error message? Can you reproduce it from the MySQL command line?
The code below is my actual program:
#include <sqlite3.h>
#include<stdlib.h>
#include<stdio.h>
//gcc -o sql1 sql1.c -lsqlite3
struct data_t {
int prev_loc, prev_pan, prev_tilt, cur_loc, cur_pan, cur_tilt;
};
struct data_t data;
static int callback1(void *not_used, int argc, char **argv, char **colname)
{
int cur_pos;
cur_pos = atoi((const char*)argv[0]); //argv[0] is the ID to be polled if it changed
data.cur_loc = cur_pos;
return 0;
}
static int callback2(void *not_used, int argc, char **argv, char **colname)
{
int x, y;
x = atoi((const char*)argv[0]); //gets info from the 2nd column (1st column is the ID)
y = atoi((const char*)argv[1]); //gets info from the 3rd column
data.cur_pan = x;
data.cur_tilt = y;
data.prev_loc = data.cur_loc;
data.prev_pan = data.cur_pan;
data.prev_tilt = data.cur_tilt;
return 0;
}
int main (int argc, char **argv)
{
data.prev_loc = 0; //initialization
data.prev_pan = 0;
data.prev_tilt = 0;
data.cur_loc = 0;
data.cur_pan = 0;
data.cur_tilt = 0;
sqlite3 *db = 0;
char *err = 0;
int input;
int r = sqlite3_open("ex1.db",&db); //ex1.db is the database that contains the 2 tables
if(r){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
while (1) {
char cmd1[1024] = "select * from try2"; // try2 is the 2nd table that contains ID
r = sqlite3_exec(db, cmd1, callback1, 0, &err); //poll table try2 if its content has changed
if( r!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", err);
}
if (data.cur_loc != data.prev_loc) { //if content of try2 has changed, get contents of try1 based from
char cmd2[1024] = ""; //try2's new value
snprintf(cmd2,sizeof(cmd2),"select val_1, val_2 from try1 where ID = %d",data.cur_loc);
r = sqlite3_exec(db, cmd2, callback2, 0, &err); //try1 is the table that has 3 columns
if( r!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", err);
}
printf("cur_pan = %d for ID = %d\n", data.cur_pan, data.cur_loc);
printf("cur_tilt = %d for ID = %d\n", data.cur_tilt, data.cur_loc);
}
}
sqlite3_close(db);
}
I run the program in one terminal, and open another terminal to update the 2nd table try2 by typing this command:
sqlite> update try2 set ID = 1 where ID = 0;
Error: database is locked
As you can see, the error says that the database is locked probably because my program is "polling" it. Is there a way to continuously poll the database and update it at the same time?