int Sqlite3_busy_handle (sqlite3*, Int (*) (void *, int), void *), there are many interpretations of this function on Google, for example:
The first parameter is a copy of the void* parameter that is passed to it when you call the Sqlite_busy_handle function, and the second argument is the number of times the callback function was called because of this lock event.
If the callback function returns 0 o'clock, it will no longer attempt to access the database again and return Sqlite_busy or sqlite_ioerr_blocked.
If the callback function returns non-0, it will continue to attempt to manipulate the database.
The function interpretation is mostly a direct translation of the English document, at least in my opinion, if there is no actual programming operation over Sqlite3_busy_handle, it will be difficult to understand the function. The following are detailed according to your own procedures.
1. First define the callback function as follows:
int callback_db (void *ptr,int count) {usleep (500000); If no lock is obtained, wait 0.5 seconds for printf ("database is Locak Now,can not write/read.\n"); Each time the callback function is executed once the message is printed, return 1; The callback function returns a value of 1, which attempts to manipulate the database continuously. }
The parameter int count is the number of times the callback function was executed, where there is no need for the count value, so there are no operations such as printing.
Char *zerrmsg = 0;int Rc;char *sql;int nrow = 0;int Ncolumn = 0;char * * AZRESULT; rc = Sqlite3_open ("moadb", &db); if (RC) {fprintf (stderr, "Can ' t Open database:%s\n", sqlite3_errmsg (db)); Sqlite3_close (db); exit (1);} Sql= "Select xxxxxx from Database"; <span style= "White-space:pre" ></span>sqlite3_busy_handler (DB, callback_db, (void *) db); Wait until you get the lock sqlite3_get_table (db, SQL, &azresult, &nrow, &ncolumn, &zerrmsg); Sqlite3_close (db);
The third parameter in Sqlite3_busy_handler is (void *) DB, which is passed to the first parameter of the callback callback function.
In the process of running the program, if there are other processes or threads reading and writing to the database, then Sqlite3_busy_handler calls the callback function continuously until the other process or thread releases the lock. Once the lock is acquired, the callback function is no longer invoked, thus performing a database select operation down. The function is to obtain a lock operation database by delaying the execution of the callback function when the lock is acquired, waiting for the other process or the thread to end the operation of the database.