Migration of SQLite Based on fs2410

Source: Internet
Author: User
Tags sql error

 I. Introduction
This article briefly introduces how to port the SQLite embedded database on the fs2410 platform. SQLite is an embedded database engine developed in C language. In the absence of confusion, this article also referred to as qlite3. The goal of a database is to store and retrieve data. In addition to basic functions such as query, add, and delete, traditional database products also provide many advanced features such as triggers, stored procedures, and data backup and recovery. However, in fact, there are not many advanced functions used, and the basic functions of databases are frequently used in applications. Therefore, in some special application scenarios, traditional databases are too bloated. In this case, embedded databases are emerging. An embedded database is a data file with the basic features of a Database. The difference between an embedded database and a traditional database is that an embedded database is directly driven by a program, while a traditional database is driven by an engine response. The size of embedded databases is usually very small, which makes embedded databases often applied to mobile devices. Because of its superior performance, we often see embedded databases in high-performance applications. SQLite is an embedded database. SQLite is designed to be as simple as possible, so it discards the complex features of traditional enterprise-level databases and only implements the functions necessary for databases. Although Simplicity is the primary goal of SQLite, it boasts outstanding functionality and performance. It has the following features: [1]: supports acid transactions (acid is the abbreviation of atomic, consistent, isolated, durable); zero configuration, no administrative configuration process is required; most sqlarray2 standards are implemented. All data is stored in a single file, and the Supported file size can be up to 2 TB. databases can be shared among machines in different byte orders. The size is small, when the optional functions are removed, the code size is smaller than kb. Even if all optional functions are added, the code size cannot exceed kb. The system overhead is small and the retrieval efficiency is high, the running speed of conventional database operations is faster than that of Client/Server databases; simple and easy-to-use API interfaces; it can be bound to TCL, Python, C/C ++, Java, Ruby, Lua, Perl, PHP, and other languages; self-contained, independent from external support; well-Annotated Code; the Code test coverage rate reaches array5. open source code can be used for any legal purposes. Thanks to some of these outstanding advantages, SQLite received 2005 of the Reilly conference held by Google and o'reilly.
Open Source Award! Because SQLite has some special advantages such as powerful functions, simple interfaces, fast speed, and small space occupation, it is particularly suitable for applications in Embedded environments. SQLite has been widely used in mobile phones, PDAs, set-top boxes, and other devices. This article describes how to port sqlite3 on the basis of the arm-Linux kernel.

Ii. porting process

First I from sqlite-amalgamation-3.6.1.tar.gz download 3.6.1 source code, SQLite official network: http://sqlite.org. I put the source code in the/home/SQLite/directory and confirm that the cross-compilation environment has been installed before porting (I installed the arm-Linux-GNU-GCC 3.4.5 ).
(1)decompress sqlite-amalgamation-3.6.1.tar.gz
/Home/SQLite/# tar zxf sqlite-amalgamation-3.6.1.tar.gz
(2) enter the decompressed SQLite directory, create a sqlite-3.6.1 directory, and enter the Directory
/Home/SQLite/sqlite-amalgamation-3.6.1 # mkdir sqlite-3.6.1
(3) create a directory to be installed. Here, I will install SQLite to the/home/directory, so I will create the/home/SQLite-arm directory.
/Home/SQLite/The sqlite-amalgamation-3.6.1 #Mkdir/home/SQLite-arm
(4) enter the following command:
Sqlite-3.6.1# ../Configure -- prefix =/home/SQLite-arm -- Host = arm-Linux-GNU 
Configure: the configuration file of the software, which generates the MAKEFILE file of the corresponding platform, -- prefix: indicates the directory where the generated files are stored. Host specifies the prefix of the Cross-compilation toolchain for the system platform where the software runs.
(5) Compile and install
A sqlite-3.6.1 # ls
Config. Log config. Status libtool makefile
Sqlite-3.6.1 #Make\ Compile the executable files that match the platform according to the makefile content
Sqlite-3.6.1 #Make install\ Install the compiled software
(6) After the above commands are executed, the: Bin, include, lib will be generated in the/home/SQLite-arm/directory.
Sqlite-3.6.1 #Ls/home/SQLite-arm
Bin include lib
(7) You can use the following commands to strip the debugging information of the dynamic library file to reduce the space occupied by the file.
Sqlite-3.6.1 #CD/home/SQLite-arm
SQLite-arm #CD lib
SQLite-arm/lib # ls
Libsqlite3.a libsqlite3.so
Libsqlite3.so. 0.8.6
Libsqlite3.la libsqlite3.so. 0
SQLite-arm/lib #Arm-Linux-GNU-strip libsqlite3.so. 0.8.6
SQLite-arm/lib # CD ../bin
SQLite-arm/bin # ls
Sqlite3
SQLite-arm/bin #Arm-Linux-GNU-strip sqlite3
(8) Finally, copy the entire/home/SQLite-arm directory to the Network File System of the board. Here, the NFS file system of the Board is in/rootfs/filesystem/
SQLite-arm/bin #CD ../../
Home #CP SQLite-arm/rootfs/filesystem/home/-(Remember to add-a to copy the soft link together.)
Home #CD/rootfs/filesystem/home/
/Rootfs/filesystem/home # ls
CaoyiSQLite-arm
/Rootfs/filesystem/home #CD SQLite-arm
/Rootfs/filesystem/home/SQLite-arm #Ls
Bin include lib
(9) copy the required dynamic library to the/lib directory of the root file system.
Rootfs/filesystem/home/SQLite-arm #CP./lib/*.../lib/-
In this way, the dynamically compiled program will not be troubled by the inability to find the library file!
This point has been transplanted.Sqlite3. The executable file is in the bin directory, the include directory contains some header files, and the lib directory contains some dynamic libraries.
Iii. Test

(1) The following # commands start with on the development board Terminal
#CD/home
# Ls
Adc fadc shyiSQLite-arm
#CD SQLite-arm/
# Ls
Bin include lib
#CD bin/
# Ls
Sqlite3
#./Sqlite3
SQLite version 3.6.1
Enter ". Help" for instructions
Enter SQL statements terminated with ";"
SQLite>
At this time, it indicates that the operation is successful, but nothing else can be done now. You only need to restart the Development Board.
(2) write two C Programs connected to the SQLite database and run them on fs2410:
Test procedure 1:
/Rootfs/filesystem/home #Vim test1.c(Write on the host)

# Include <stdio. h>
# Include "sqlite3.h"

Int main (void)
{
Sqlite3 * DB = NULL;
Int RC;

Rc = sqlite3_open ("test. DB", & dB );

If (RC ){
Fprintf (stderr, "can't open database: %

S \ n ", sqlite3_errmsg (db ));
Sqlite3_close (db );
Exit (1 );
}
Else {
Printf ("open test. DB successfully! \ N ");
}

Sqlite3_close (db );

Return 0;
}

Cross-Compilation:

/Rootfs/filesystem/home #Arm-Linux-GNU-gcc-I./SQLite-arm/include/-L./SQLite-arm/lib/test. C-o Test-lsqlite3
-I specifies the path of the header file to be included,-l specifies the path of the dynamic library, and-lsqlite3 specifies the name of the dynamic library. Run the following command in the/home directory of the terminal's root file system:
#./Test
Open test. DB successfully!

A test. DB file is displayed.
Then we write a C language program that contains SQL statements such as connecting to the database, creating tables, and inserting data.
Test Procedure 2:
/Rootfs/filesystem/home #Vim test2.c(This is an official test program written on the host)

# Include <stdio. h>
# Include "sqlite3.h"
 
Static int callback (void * notused, int argc, char ** argv, char

** Azcolname)
{
Int I;
For (I = 0; I <argc; I ++ ){
Printf ("% s = % s \ n", azcolname, argv [I]? Argv [I]:

"Null ");
}
Printf ("\ n ");
Return 0;
}

Int main (INT argc, char ** argv)
{
Sqlite3 * dB;
Char * zerrmsg = 0;
Int RC;

If (argc! = 3 ){
Fprintf (stderr, "Usage: % s database_name SQL-

Statement \ n ", ARG
V [0]);
Exit (1 );
}
Rc = sqlite3_open (argv [1], & dB );
If (RC ){
Fprintf (stderr, "can't open database: % s \ n ",

Sqlite3_errmsg (d
B ));
Sqlite3_close (db );
Exit (1 );
}
Rc = sqlite3_exec (dB, argv [2], callback, 0, & zerrmsg );
If (RC! = Sqlite_ OK ){
Fprintf (stderr, "SQL error: % s \ n", zerrmsg );
Sqlite3_free (zerrmsg );
}
Sqlite3_close (db );
 
Return 0;
}

Cross-Compilation:
/Rootfs/filesystem/home #Arm-Linux-GNU-gcc-I./SQLite-arm/include/-L./SQLite-arm/lib/test2.c-O Test2-lsqlite3

Next we can test the test program. The test program accepts two parameters: the first parameter is the database file name, and the second parameter is the SQL statement to be executed. The sqlite3 APIs in the program are mainly related to four parts: sqlite3_open () of row 27th, sqlite3_exec () of row 33rd, sqlite3_close () of row 30th and row 38th (), sqlite3_free () of Row 3 (). For more information about sqlite3 API interfaces, see the previous articles.
The following is the complete process for testing the test program.
#./Test2 test. DB "create table Tb (name varchar (10), number smallint );"
#./Test2 test. DB "insert into TB values ('caoyi', 1 );"
#./Test2 test. DB "insert into TB values ('ayyun ', 2 );"
#./Test2 test. DB "select * from TB ;"
@ = Caoyi
@ = 1

@ = Huayun
@ = 2

Explain the test command used above: the first command is in test. a tb table is created in the database file dB. The table contains two fields. The field name is a variable-length string and the field number type is smallint; the second command inserts a record ('caoyi', 1) into the tbl0 table of the database. The third command inserts a record ('huayun ', 2) into the TB table of the database ); the fourth command is to query all the content in Table TB. As expected, this command prints two newly inserted records except the database. From this we can draw a conclusion that these commands have indeed all worked as expected. At the same time, after inserting the data shown above into the database, we can see that the size of the database file test. DB has changed:
Before:
Rootfs/filesystem/home # ll-h test. DB
-RW-r -- 1 Root 1 test. DB
Now:
Rootfs/filesystem/home # ll-h test. DB
-RW-r -- 1 Root 2.0 K test. DB

The size of the database file test. DB is 2 K. Since then, the sqlite3 database has been transplanted on the fs2410 evaluation board. Test results show that the database works properly.
Iv. Conclusion
SQLite is an excellent embedded database. This article describes in detail how to port sqlite3 to the arm-Linux platform, and a simple test of the transplanted sqlite3. SQLite is applicable to embedded mobile device environments because of its powerful functions, high efficiency, zero configuration, and small size. Therefore, the transplantation details of sqlite3 in this article are of positive significance. Sqlite3 is highly portable because it is developed in C language. The method discussed in this article is slightly modified and can be applied to other operating system platforms.

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.