C Programming Interface of SQlite database (2) database connection -- Reading Notes of Using SQlite

Source: Internet
Author: User
Tags sqlite db

C Programming Interface of SQlite database (2) database connection by Drizzle QQ: 253786989
2012-02-03

Library Initialization

int sqlite3_initialize(void);int sqlite3_shutdown(void);

Before using SQlite Library, you should first call the sqlite3_initialize function, which allocates resources and initializes some necessary data structures. Another function used with this function is sqlite3_shutdown, which is used to release resources allocated by sqlite3_initialize. However, in many customers' applications, sqlite3_open or other major API functions are usually called directly. These functions will automatically initialize the SQlite library (if it has not been initialized ). However, we recommend that the customer application call these two APIs functions to complete the initialization and final cleaning of the SQlite library.

Open Database

int sqlite3_open(  const char *filename,   /* Database filename (UTF-8) */  sqlite3 **ppDb          /* OUT: SQLite db handle */);int sqlite3_open16(  const void *filename,   /* Database filename (UTF-16) */  sqlite3 **ppDb          /* OUT: SQLite db handle */);int sqlite3_open_v2(  const char *filename,   /* Database filename (UTF-8) */  sqlite3 **ppDb,         /* OUT: SQLite db handle */  int flags,              /* Flags */  const char *zVfs        /* Name of VFS module to use */);

Before executing any SQL statement, you must first connect to a database, that is, open or create a new SQlite3 database file. The sqlite3_open function connects to the database. It has three versions above. The sqlite3_open function assumes that the SQlite3 database file name is UTF-8 encoding, sqlite3_open_v2 is its enhanced version. The sqlite3_open16 function assumes that the SQlite3 database file name is encoded in UTF-16 (Unicode wide character.

For all three functions, the filename parameter is the name string of the SQlite3 database file to be connected. The ppDb parameter looks a little complicated. It is a pointer to a pointer. When you call the sqlite3_open_xxx function, the function allocates a new SQlite3 data structure, initializes it, and points the pointer ppDb to it. Therefore, the customer application can connect to the database named filename through the sqlite3_open_xxx function, and return a pointer to the data structure of the database through the ppDb parameter.

Sample Code:

  sqlite3 *pDB = NULL;  Status rc = sqlite3_open("database.sqlite3", &pDB);

For sqlite3_open and sqlite3_open16 functions, if it is possible to open the database in readable and writable mode, otherwise, the database will be opened in read-only mode. If the database file to be opened does not exist, create a new one. For sqlite3_open_v2 functions, the situation is more complicated, because the powerful functions of this v2 version can be used to control the way to open (connect) The database, it is done through its flags parameter. The sqlite3_open_v2 function only supports UTF-8-encoded SQlite3 database files.

If flags is set to SQLITE_OPEN_READONLY, The SQlite3 database file is opened in read-only mode. If the database file does not exist, the sqlite3_open_v2 function fails to be executed and an error is returned. If flags is set to SQLITE_OPEN_READWRITE, The SQlite3 database file is opened in readable and writable mode. If the database file itself is set to write protection by the operating system, it is opened in read-only mode. If the database file does not exist, the sqlite3_open_v2 function fails to be executed and an error is returned. If flags is set to SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, The SQlite3 database file is opened in readable and writable mode. If the database file does not exist, a new one is created. This is also the default behavior of sqlite3_open and sqlite3_open16 functions. In addition, flags can be set as other flags. For more information, see the official SQlite documentation.

The zVfs parameter allows the customer's application to name a Virtual File System module to connect to the database. VFS is an abstraction layer between SQlite library and underlying storage systems (such as a file system). Generally, a customer application can simply pass a NULL pointer to this parameter, to use the default VFS module.

For the SQlite3 database file encoded by the UTF-8, it is recommended to use sqlite3_open_v2 function to connect, it can open the database file and process the operation of more control.

There is no standard definition for the extension of SQlite3 database files. The popular options are. sqlite3,. db, and. DB3. However, on Windows systems, it is not recommended to use. sdb as the extension of SQlite3 database files. It is said that this will lead to a significant reduction in IO speed, because the. sdb extension has its own special purpose.

Close Database

int sqlite3_close(sqlite3 *);

After using the SQlite database, you need to call the sqlite3_close function to close the database connection, release the memory associated with the data structure, and delete all temporary data items. If there are still some unfinished (nonfinalized) SQL statements before calling the sqlite3_close function to close the database, the sqlite3_close function will return the SQLITE_BUSY error. The client programmer needs all the finalize pre-processing statements (prepared statement) and then calls sqlite3_close again.

Sample Code

#include "sqlite3.h"#include <stdlib.h>int main( int argc, char **argv ){    Char *file = "database.sqlite3";    sqlite3 *pDB = NULL;    int rc = 0;    sqlite3_initialize( );    rc = sqlite3_open_v2( file, &db, SQLITE_OPEN_READWRITE |          SQLITE_OPEN_CREATE, NULL );    if ( rc != SQLITE_OK)     {sqlite3_close( db );exit( -1 );    }        /*  perform database operations  */        sqlite3_close( db );    sqlite3_shutdown( )}

C Programming Interface of SQlite database (2) database connection by Drizzle QQ: 253786989
2012-02-03

Related Article

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.