Core c api:
Query encapsulation:
Connection and disconnection:
In fact, it is to open the database. You can use sqlite3_open_v2 (), sqlite3_open (), and sqlite3 _ open16 () functions (). Among them, sqlite3_open_v2 () function is the most powerful and the latest function. Use this function whenever possible.
Open the database function declaration:
Int sqlite3_open_v2 (const char * filename, sqlite3 * ppdb, int flags, const char * zvfs); where, filename database file name, ppdb database handle, flag Flag,
Zvfs is the name of the VFS file to be used. If the database file does not exist and the third parameter includes the sqlite_open _ create flag, the function will open a new database file. Otherwise, the system will return
Error. Flags include the sqlite_open_create, sqlite_open_readonly, and sqlite_open_readwrite vectors. When you open a database in read-only and read-write modes, both of these options are required.
The database file already exists. Otherwise, an error will be returned, which is a traditional open. The flags parameter can also be combined with sqlite_open_nomutex, sqlite_open_fullmute, sqlite_open_sharedcache,
Sqlite_open_privatecache further controls the transaction behavior of the database handle. The zvfs parameter allows you to call the sqlite3_vfs method to override the default Operating System Interface.
After opening, sqlite3_open_v2 () initializes the struct that has the ppdb parameter passed to it, which can be called an implicit handle. This handle represents a connection to the database. This connection may have multiple databases attached, representing the transaction context environment.
Int sqlite3_open (const char * filename, sqlite3 * ppdb );
Int sqlite3_open (const void * filename, sqlite3 * ppdb );
Disable the database function declaration:
Int sqlite3_close (sqlite3 *); if the database is successfully closed, the sqlite_busy error is returned and a prompt is displayed. If there are opened transactions on the connection, the transaction will be automatically rolled back.
Run the query:
Function: sqlite3_exec (), called a convenient function, encapsulates many tasks.
Function declaration:
Int sqlite3_exec (SQLite *, const char * SQL, sqlite_callback, void * data, char ** errmmsg); SQLite * indicates the database opened; SQL indicates the executed SQL statement; callback
Call the function; void * data is the first parameter of the callback function pointing to the application provided to the callback function.ProgramSpecific data is also the first parameter of the callback function. errmsg is the error message, which is a pointer to the error message string.
Sqlite_exec () has two error message sources: returned value and readable string errmsg.
NOTE: If errmsg is provided, the memory used to create error messages is distributed on the stack. Therefore, check whether the value is null after the call. If an error occurs, use sqlite3_free () to release the memory occupied by errmsg.
Use sqlite3 _ exec () in a simple commandCode:
Int main (INT argc, char ** argv)
{
Sqlite3 * dB;
Char * zerr;
Nt rc;
Char * SQL;
Rc = sqlite3_open_v2 ("test. DB", & dB );
If (RC ){
Fprintf (stderr, "can't open database: % s \ n", sqlite3_errmsg (db ));
Sqlite3_close (db );
Exit (1 );
}
SQL = "CREATE TABLE episodes (ID int, Name text )";
Rc = sqlite3_exec (dB, SQL, null, null, & zerr );
If (RC! = Sqlite_ OK ){
If (zerr! = NULL ){
Fprintf (stderr, "SQL error: % s \ n", zerr );
Sqlite3_free (zerr );
}
}
SQL = "insert into episodes values (10, the dinner party ')";
Rc = sqlite3_exec (dB, SQL, null, null, & zerr );
Sqlite3_close (db );
Return 0;
}
Callback function declaration:
Typedef int (* sqlite3_callback) (void *, Int, char **, char **); void * indicates the data provided by the Fourth parameter sqlite3_exec, int indicates the number of fields. Char ** is the string array of the field name in the row, and char ** indicates the string array of the field ing.
Sqlite3_exec () allows you to execute a batch of commands and collect all returned data through the callback interface.