Linux Kernel database sqlite3 porting and simple operations

Source: Internet
Author: User
Embedded Linux-based databases mainly include SQLite, Firebird, Berkeley dB, and ExtremeDB databases. Features: • Firebird is a relational database with powerful functions, supports stored procedures, SQL compatibility, and other • SQLite relational databases, which are small in size and support acid transactions • There is no concept of database servers in Berkeley dB, its Library is directly linked to the application • ExtremeDB is a memory database, and the source code with high running efficiency SQLite is C. Its source code is completely open and is a lightweight embedded database. SQLite has the following features:

No configuration. No installation or configuration management is required;

A complete database stored in a single disk file;

Database files can be freely shared among machines with different byte sequences;

The database size is 2 TB;

Small enough. The source code contains approximately 30 thousand lines of C code, kb;

Faster data operations than most popular databases;

This database operation is relatively simple. First install the database:

 

This is very simple. You can find the corresponding linux In the download directory of the sqlitehomepage http://www.sqlite.org/download.html. Decompress the downloaded package and execute

Configure.

Run sudo make & make install

Wait for a while before the installation is complete.

After completion, let's take a look at the path of the sqlite3 execution file.

 

In the/usr/local/bin directory.

For future convenience, you can add this directory to the environment variable. First, open sudo Vim/environment to add the path here. How can I port sqlite3 to the Development Board?

1. Remove the sqlite3 calling Information in the/root/sqlite3.3.6 Directory: (commonly known as slimming)

# Arm-Linux-strip sqlite3

2. Download sqlite3 to the/usr/bin directory of the Development Board:

Find the libsqlite3.so. 0 and libsqlite3.so. 0.8.6 library files in the/usr/lib directory of the PC, and copy them to the/usr/lib directory of the Development Board after removing the call information:

Arm-Linux-strip libsqlite3.so. 0 (/home/Linux/SQLite/LIB)

Arm-Linux-strip libsqlite3.so. 0.8.6 (/home/Linux/SQLite/LIB)

The purpose of adding the CP-arms libsqlite3.so. 0.8.6/usr/lib to the libsqlite3.so. 0.8.6/usr/lib is to copy the source database. Do not copy only one link. This is a tragedy...

 

 

After the above steps, the SQLite database is available on the Development Board.

Some basic operations of SQLite can be found on the Internet. SQL statements are common, so it is important that sqlite3 provides some database operation interfaces:

 

Int sqlite3_open (char * path, sqlite3 ** dB );

Function: Enable the SQLite database.

Path: database file path

DB: pointer to the SQLite handle

Return Value: 0 is returned for success, and error code (non-zero value) is returned for failure)

Int sqlite3_close (sqlite3 * dB );

Function: Disable the SQLite database.

Return Value: 0 is returned for success, and error code is returned for failure.

Const char * sqlite3_errmg (sqlite3 * dB );

Returned value: an error message is returned.

 

Gcc-O test. C-lsqlite3

 

} Callback Function Definition: typedef int (* sqlite3_callback) (void * para, int f_num, char ** f_value, char ** f_name );}

Function: the callback function is automatically executed every time a record is found.

Para: The parameter passed to the callback function.

F_num: number of fields contained in the record

F_value: A pointer array containing the values of each field

F_name: pointer array containing the name of each field

Return Value: 0 is returned for success, and-1 is returned for failure.

Int sqlite3_exec (sqlite3 * dB, const char * SQL, sqlite3_callback callback, void *, char ** errmsg );

Function: Performs SQL operations.

DB: Database handle

SQL: SQL statements

Callback: callback function

Errmsg: error message pointer address

Return Value: 0 is returned for success, and error code is returned for failure.

 

For example, define a callback function to print the names and values of all fields in the record.

int  callback(void *para, intf_num,  char **f_value, char **f_name){      int  i;      printf(“*****************************\n”);      for (i=0; i<f_num; i++)      {            printf(“%s :  %s\n”,  f_name[i], f_value[i]);      }      return  0;}
sqlite3 *db;char  *errmsg;          ……if  (sqlite3_exec(db, “select  *  from  table”,  callback,  NULL, &errmsg) !=  SQLITE_OK){     printf(“error :  %s\n”,  errmsg);     exit(-1); } ……

 

Run the SQL statement int sqlite3_get_table (sqlite3 * dB, const char * SQL, char *** resultp, int * nrow, int * ncolumn, char ** errmsg) without using the callback function ); function: Execute SQL operations

Function parameters:

DB: Database handle

SQL: SQL statements

Resultp: a pointer to the SQL Execution result.

Nrow: number of records that meet the condition

Ncolumn: number of fields contained in each record

Errmsg: error message pointer address

Return Value: 0 is returned for success, and error code is returned for failure.

 

Sqlite3 * dB; char * errmsg, ** resultp; int nrow, ncolumn, I, j, index ;...... If (sqlite3_exec (dB, "select * from table", & resultp, & nrow, & ncolumn, & errmsg )! = Sqlite_ OK) {printf ("error: % s \ n", errmsg); exit (-1) ;}index = ncolumn; // subscript of the first field in the first record for (I = 0; I <nrow; I ++) {for (j = 0; j <ncolumn; j ++) {printf ("% s: % s \ n", resultp [J], resultp [index ++]);}

 

 

With these functions, database operations are basically OK.

 

 

 

 

 

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.