How to operate the database sqlite3 in C/C ++ in Linux

Source: Internet
Author: User

0. Introduction

This article describes how to call the SQLite function interface in C/C ++ to manage databases,

This includes creating databases, creating tables, inserting data, querying data, and deleting data.

1. Description

Here we assume that you have compiled the SQLite library file:

Libsqlite3.a libsqlite3.la libsqlite3.so libsqlite3.so. 0 libsqlite3.so. 0.8.6 pkgconfig

And executable files: sqlite3

Assume that the installation directory of your sqlite3 is in the/usr/local/sqlite3 directory.

If not, copy your installation file to the directory/usr/local/sqlite3,

In this way, the following operations are more unified to reduce the probability of errors.

For example: [root @ localhost Home] # cp-RF sqlite-3.3.8-ix86 // usr/local/sqlite3

Here suppose/home/sqlite-3.3.8-ix86/is your installation directory, that is, your SQLite is installed here

In this case, the Directory of the sqlite3 library is:/usr/local/sqlite3/lib

The Directory of the executable sqlite3 file is/usr/local/sqlite3/bin.

The Directory of the header file sqlite3.h is:/usr/local/sqlite3/include

Haola, Now we start our sqlite3 programming journey in Linux.

2. Start

Now we will perform a test.

Now let's write a C/C ++ program to call the SQLite API function.

 
The following example shows how to use the C/C ++ interface of SQLite. The database name is obtained by the first parameter and the second or more parameters are SQL.
Execute the statement. This function calls sqlite3_open () to open the database in 16 rows, and sqlite3_close () closes the database connection in 25 rows.

[Root @ localhost temp] # vi opendbsqlite. c

Press the I key to switch to the input mode and enter the following code:

 

// Name: opendbsqlite. c
// This prog is used to test C/C ++ API for sqlite3.it is very simple, ha!
// Author: zieckey All Rights Reserved.
// Data: 2006/11/13
# Include <stdio. h>
# Include <sqlite3.h>
Int main (void)
{
Sqlite3 * DB = NULL;
Char * zerrmsg = 0;
Int RC;
// Open the specified database file. If it does not exist, a database file with the same name will be created.
Rc = sqlite3_open ("zieckey. DB", & dB );
If (RC)
{
Fprintf (stderr, "can't open database: % s
", Sqlite3_errmsg (db ));
Sqlite3_close (db );
Exit (1 );
}
Else printf ("You have opened a sqlite3 database named zieckey. DB successfully!
Congratulations! Have fun! ^-^
");
Sqlite3_close (db); // close the database
Return 0;
}

Exit and save. (After the code is entered, Press ESC and enter: WQ. Press enter to pull it)

Haola, now compiled: [root @ localhost te

 

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.