Sqlite3 Sqlite3_busy_handler use of the detailed

Source: Internet
Author: User

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.


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.