C language programming based on SQLite Database

Source: Internet
Author: User
Sqlite provides some C function interfaces that you can use to operate databases. By using these interfaces, pass some standard SQL statements (in char * type)

Sqlite provides some C function interfaces that you can use to operate databases. By using these interfaces, pass some standard SQL statements (in char * type)

I. SQLITE operations

SqliteProvides some C function interfaces that you can use to operate databases. By using these interfaces, you can pass some standard SQL statements (in char * type) to the sqlite function, and sqlite will operate the database for you.

Sqlite and MS access is the same as a file database, that is to say, a database is a file, this database can create a lot of tables, you can create indexes, triggers, etc.,, what it actually gets is a file. Back up this file to back up the entire database.

Sqlite does not need any database engine, which means that if you need sqlite to save some user data, you don't even need to install the database (if you have to install sqlserver to run a small software, it's too dark ).

The following describes basic database operations.

1. Basic Process
(1) Key Data Structure
The most common type in sqlite is sqlite3. Since the database is opened, sqlite needs to prepare memory for this type until the database is closed. This type is used throughout the process. When the database is opened, this type of variable represents the database you want to operate on. The following is a detailed introduction.

(2) Open the database
Int sqlite3_open (file name, sqlite3 **);

Use this function to start database operations.

You need to input two parameters. One is the database file name, for example, c: \ DongChunGuang_Database.db.

The file name does not need to exist. If the file does not exist, sqlite will automatically create it. If it exists, try to open it as a database file.

The sqlite3 ** parameter is the key data structure mentioned above. Do not close the underlying details of this structure.

The Return Value of the function indicates whether the operation is correct. If it is SQLITE_ OK, the operation is normal. Related return values sqlite defines some macros. For more information about these macros, see the sqlite3.h file. There are detailed definitions in it (by the way, sqlite3 code annotation rate claims to be very high, but it is indeed very high. As long as you can read English, sqlite can help you learn a lot ).

The following describes how to shut down the database and give a reference code.

(3) shut down the database
Int sqlite3_close (sqlite3 *);

If you have enabled a database with sqlite3_open, do not forget to use this function to close the database.

The following is a simple code:

Extern "C"

# Include "./sqlite3.h"

};

Int main (int, char **)

Sqlite3 * db = NULL; // declare sqlite key structure pointer

Int result;

// Open the database

// You need to pass in the pointer of the database, because the sqlite3_open function needs to allocate memory for this pointer, And the db pointer must point to this memory Zone

Result = sqlite3_open ("c: \ Dcg_database.db", & db );

If (result! = SQLITE_ OK)

{

// Failed to open the database

Return-1;

// Database operation code

//...

// The database is successfully opened.

// Close the database

Sqlite3_close (db );

Return 0;

This is a database operation process.

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.