C language interface of SQLite

Source: Internet
Author: User
Tags sql error
I am writing a software program on the Linux platform and need to use a simple database. Mysql is a good database, but its data is stored on the server. The basic functions I want are also

I am writing a software program on the Linux platform and need to use a simple database. Mysql is a good database, but its data is stored on the server. The basic functions I want are also

I am writing a software program on the Linux platform and need to use a simple database. Mysql is a good database, but its data is stored on the server. The basic function I want is to create a local database file using program C, and then perform basic SQL operations on the database file. just like programming the DAO Database Based on VC6.0 on Windows platform (creating a local file. mdb ).
I found an open-source and free database development tool named sqlite on the Internet. There are many introductions on sqlite on the Internet. For details, see the official website :. I found that sqlite is exactly what I need. summarize the following features:
1. Open source code
2. Special program. In Windows, sqlite.exe is only less than kb.
3. Supports most SQL commands at extremely fast speeds
4. directly create a xxx. db, which is a database without server support.
5. Simple C language API

Based on the above points, we can see that sqlite is powerful and outstanding. The open source code is very small and the program is very small. You can even directly compile "gcc-o sqlite3 *" across platforms. How wonderful it is to integrate this into a sneak system!

Install sqlite3 and Its Development Kit on Ubuntu6.10:
# Sudo apt-get install sqlite3 libsqlite3-dev

This article introduces how to use sqlite:

The following is my summary of the APIs for sqlite3 and C interfaces.
I mainly use the following functions (header file sqlite3.h ):
Int sqlite3_open (const char *, sqlite3 **); // open a database
Int sqlite3_close (sqlite3 *); // close
Int sqlite3_exec (sqlite3 *, const char * SQL, sqlite_callback, void *, char **); // Execute
Int sqlite3_get_table (sqlite3 *, const char * SQL, char *** result, int * nrow, int * ncolumn, char ** errmsg );
// Result stores the queried data in the form of an array, first the table name, and then the data;
// Nrow/ncolumn are the number of rows/columns in the result set returned by the query statement. If no result is found, 0 is returned.
Sqlite3_errcode () is usually used to obtain the error code returned by the recently called API.
Sqlite3_errmsg () is used to obtain the text description corresponding to these error codes.

Exec error code
# Define SQLITE_ OK 0/* Successful result */
# Define SQLITE_ERROR 1/* SQL error or missing database */
# Define SQLITE_INTERNAL 2/* An internal logic error in SQLite */
# Define SQLITE_PERM 3/* Access permission denied */
# Define SQLITE_ABORT 4/* Callback routine requested an abort */
# Define SQLITE_BUSY 5/* The database file is locked */
# Define SQLITE_LOCKED 6/* A table in the database is locked */
# Define SQLITE_NOMEM 7/* A malloc () failed */
# Define SQLITE_READONLY 8/* Attempt to write a readonly database */
# Define SQLITE_INTERRUPT 9/* Operation terminated by sqlite_interrupt ()*/
# Define SQLITE_IOERR 10/* Some kind of disk I/O error occurred */
# Define sqlite_0000upt 11/* The database disk image is malformed */
# Define SQLITE_NOTFOUND 12/* (Internal Only) Table or record not found */
# Define SQLITE_FULL 13/* Insertion failed because database is full */
# Define SQLITE_CANTOPEN 14/* Unable to open the database file */
# Define SQLITE_PROTOCOL 15/* Database lock protocol error */
# Define SQLITE_EMPTY 16/* (Internal Only) Database table is empty */
# Define SQLITE_SCHEMA 17/* The database schema changed */
# Define SQLITE_TOOBIG 18/* Too much data for one row of a table */
# Define SQLITE_CONSTRAINT 19/* Abort due to contraint violation */
# Define SQLITE_MISMATCH 20/* Data type mismatch */
# Define SQLITE_MISUSE 21/* Library used incorrectly */
# Define SQLITE_NOLFS 22/* Uses OS features not supported on host */
# Define SQLITE_AUTH 23/* Authorization denied */
# Define SQLITE_ROW 100/* sqlite_step () has another row ready */
# Define SQLITE_DONE 101/* sqlite_step () has finished executing */
Application instance:

Definition
Sqlite3 * db = NULL;
Char * errMsg = NULL;
Char SQL _cmd [2, 200];

Open the file:
Int rc = sqlite3_open ("xxx. db", & db );
If (rc) // open failed
Printf ("Open database failed! \ N ");
Else
Printf ("create the database successful! \ N ");

Create a table:
Sprintf (SQL _cmd, "CREATE TABLE datapro (package INTEGER, offset \
INTEGER, lklen INTEGER, base INTEHER, link INTEGER, err INTEGER );");
Rc = sqlite3_exec (db, SQL _cmd, & eMsg); // create a table datapro
If (rc = SQLITE_ OK) // The table is successfully created.
Printf ("create the chn_to_eng table successful! \ N ");
Else
Printf ("% s \ n", eMsg );
Add data:
Sprintf (SQL _cmd, "INSERT INTO datapro VALUES (% d, % d);", 3 );
Rc = sqlite3_exec (pro_db, pro_sqlcmd, 0, & eMsg );

Query:
Int nrow = 0, ncolumn = 0;
Char ** azResult; // Save the result
SQL = "SELECT * FROM datapro ";
Sqlite3_get_table (db, SQL, & azResult, & nrow, & ncolumn, & eMsg );
// Where nrow is the number of rows and ncolum is the number of Columns
Printf ("\ nThe result of querying is: \ n ");
For (int I = 1; I {
For (int j = 0; j Printf ("% s", azResult [I * ncolumn + j]);
Printf ("\ n ");
}

Delete:
Sprintf (SQL _cmd, "DELETE FROM datapro WHERE package = 1 ;");
Rc = sqlite3_exec (db, SQL, 0, 0, & eMsg );

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.