C Programming Interface of SQlite database (8) Utility Functions -- Reading Notes of Using SQlite

Source: Internet
Author: User
Tags sha1 hash

C Programming Interface of SQlite database (8)
Utility Functions by Drizzle QQ: 253786989

SQLite also has some useful tool functions, although they may not be required for basic database tasks.

Version Management (Version Management)

(1)

const char *sqlite3_libversion(void);

Returns the version of the SQLite library in the form of UTF-8 encoded strings. SQLite also defines a macro: SQLITE_VERSION, whose value is also a UTF-8 encoded string representing the SQLite library version.

For example: # define SQLITE_VERSION "3.7.10"

(2)

Returns the SQLite database version in the form of an integer in the format of MNNNPPP. M is the primary version number. For the SQLite3 database, the M value is 3. N is the next version number, and P represents the issue point.

The macro defines SQLITE_VERSION_NUMBER, which has the same function as sqlite3_libversion_number.

For example: # define SQLITE_VERSION_NUMBER 3007010

(3)

const char *sqlite3_sourceid(void);

Returns a string pointer pointing to a string containing a date, timestamp, and a SHA1 hash value. The macro-defined SQLITE_SOURCE_ID function is the same as the function.

For example: # define SQLITE_SOURCE_ID "2012-01-16 16:56:31 93aa17d866873e11dde5ffbefe74497f229977c1"

Programmers can use the above functions and corresponding macro definitions to verify the version of the SQLite library. Macro definition comes from the sqlite. h header file, and the version number obtained by function call comes from the SQLite library linked to the program.

For example:

if(SQLITE_VERSION_NUMBER > sqlite3_libversion_number()) {    /* library too old; report error and exit. */}

In this program, the SQLITE_VERSION_NUMBER value comes from the sqlite. h header file used by our customer application during compilation. The version number returned by the sqlite3_libversion_number () function call comes from the version of the library connected when the application is running. Therefore, in our applications, we can compare the above Code to determine whether the connected library is lower than the version used during application compilation, if yes, an error report or exit the program.

Memory Management (Memory Management)

When SQLite needs to dynamically allocate a piece of memory, it usually calls the default memory processing function of the underlying operating system, so that SQLite can allocate memory from the application heap. However, programmers can use the sqlite3_config function to configure SQLite for internal memory management. This is very important for devices with limited memory such as embedded devices and handheld terminals. Excessive memory allocation will cause problems to system stability.

(1)

void *sqlite3_malloc(int);

Allocate a buffer of the byte size specified by the parameter and return the buffer pointer. If the memory allocation fails, NULL is returned. The allocated memory is always 8-byte (64-bit) alignment. This function is a substitute for the c standard library function malloc.

(2)

void *sqlite3_realloc(void*, int);

This function is used to re-adjust the memory allocation. The first parameter is the buffer pointer returned by the sqlite3_malloc function, and the second parameter is the adjusted buffer byte size. The sqlite3_realloc function allocates a new buffer based on the size of the byte specified by its 2nd parameters. Then, copy as much content as possible from the old buffer to which the 1st parameters point to the new buffer. Release the old buffer and return the new buffer pointer. If the new buffer Allocation fails, NULL is returned and the old buffer is not released.

If the sqlite3_realloc function is called to pass 0 or negative values to the 2nd parameters, the function call is equivalent to calling the sqlite3_free function to release the memory.

The sqlite3_realloc function is a substitute for the c standard library function realloc.

(3)

void sqlite3_free(void*);

Releases the memory allocated by the sqlite3_malloc function or sqlite3_realloc function. This function is a substitute for the c standard library function free.

Because sqlite3_xxx memory management functions are operated in the SQLite internal environment, using these functions is more portable and reliable than using the underlying memory management functions of the operating system.

C Programming Interface of SQlite database (8)
Utility Functions by Drizzle QQ: 253786989

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.