Recently, because of the school project, we were also engaged in database-related things. We used SQLITE things before and used them for review.
Sqlite is a database for embedded systems. It is compiled with only 200 kb and supports 2 TB of data records. It is a good database engine for embedded devices. This article uses a small example to illustrate how to call the Sqlite API in C and C ++ to create, insert, and query databases. The development environment in this article is (Redhat9.0 + Qtopia2.1.2 + Sqlite3)
Install Sqlite3:
Download the Sqlite3.2.2 source code from www.sqlite.org and follow the steps in Readme:
Tar xzf sqlite3.2.2.tar.gz Mkdir bld Cd bld ../Sqlite3.2.2/configure Make Make install |
Run the sqlite3 test. db command in shell to check whether the installation is successful.
Create a database:
Sqlite3 * pDB = NULL; Char * errMsg = NULL; // Open a database. If the database does not exist, create a database file named databaseName. Int rc = sqlite3_open (databaseName, & pDB ); If (rc) { Cout <"Open the database" <databaseName <"failed" <endl; } // If the table is successfully created, add the table Else { Cout <"create the database successful! "<Endl; // Creat the table Int I; For (I = 1; I { } // Insert a table, and the returned value is SQLITE_ OK. Otherwise, an error is returned. // Function parameter: the first is the pointer to the database, and the second sentence is the SQL command string. // The third parameter is the callback function, which is useless here. The fourth parameter is the callback function. // The first parameter in, and the fifth parameter is the error message. Rc = sqlite3_exec (pDB, "create table chn_to_eng (chinese QString, english QString)", 0, 0, & errMsg );
If (rc = SQLITE_ OK) Cout <"create the chn_to_eng table successful! "<Endl; Else Cout <errMsg <endl; // Same as above, insert another table Rc = sqlite3_exec (PDB, "create table eng_to_chn (English qstring, Chinese qstring)", 0, 0, & errmsg );
If (rc = sqlite_ OK) Cout <"create the eng_to_chn Table successful! "<Endl; Else Cout <errmsg <Endl; } ,,,,,, // Add data to the table Char CHN [] = "..."; Char Eng [] = "..."; Char value [500]; // Define a parameter SQL command, where CHN and ENG are the data to be inserted. Sprintf (value, "insert into chn_to_eng (Chinese, English) values ('% s',' % s')", CHN, Eng );
// Use the SQLite C/C ++ API to create and adjust a database. Rc = sqlite3_exec (PDB, Value, 0, 0, & errmsg ); // Query a record Char value [500]; // Define a query statement. The condition is the Chinese record when the English is target. // Print_result_cb is the callback function. You can obtain the query result. See the following for details. Sprintf (value, "select Chinese from eng_to_chn where English = '% S'", target ); Rc = sqlite3_exec (PDB, Value, Print_result_cb, 0, & errmsg ); If (rc = sqlite_ OK) { // # Ifdef_debug Cout <"select the record successful! "<Endl; // # Endif } Else { // # Ifdef_debug Cout <errmsg <Endl; // # Endif Return false; } ....... } // Write the callback function print_result_cb. Data is the fourth parameter in sqlite3_exec, the second parameter is the number of columns, and the third parameter is the column name, the fourth is the query result. These two functions output all the query results.
Int print_result_cb (void * data, int n_columns, char ** column_values, Char ** column_names) { Static int column_names_printed = 0; Int I; If (! Column_names_printed ){ Print_row (n_columns, column_names ); Column_names_printed = 1; } Print_row (n_columns, column_values ); Return 0; } Void print_row (int n_values, char ** values) { Int I; For (I = 0; I <n_values; ++ I ){ If (I> 0 ){ Printf ("\ t "); } Printf ("% s", values [I]); } Printf ("\ n "); } |
This is the general process. For details, refer to the sqlite api function description. For details, see www.sqlite.org.
Void print_row (int n_values, char ** values) { Int I; For (I = 0; I <n_values; ++ I ){ If (I> 0 ){ Printf ("\ t "); } Printf ("% s", values [I]); } Printf ("\ n "); } |
Void print_row (int n_values, char ** values) { Int I; For (I = 0; I <n_values; ++ I ){ If (I> 0 ){ Printf ("\ t "); } Printf ("% s", values [I]); } Printf ("\ n "); } |
Char value [500]; // Define a parameter SQL command, where chn and eng are the data to be inserted. Sprintf (value, "insert into chn_to_eng (chinese, english) VALUES ('% s',' % s')", chn, eng );
// Use the sqlite c/C ++ API to create and adjust a database. Rc = sqlite3_exec (pDB, Value, 0, 0, & errMsg ); // Query a record Char value [500]; // Define a query statement. The condition is the Chinese record when the english is target. // Print_result_cb is the callback function. You can obtain the query result. See the following for details. Sprintf (value, "SELECT chinese FROM eng_to_chn where english = '% S'", target ); Rc = sqlite3_exec (pDB, Value, Print_result_cb, 0, & errMsg ); If (rc = SQLITE_ OK) { // # Ifdef_debug Cout <"select the record successful! "<Endl; // # Endif } Else { // # Ifdef_debug Cout <errMsg <endl; // # Endif Return false; } ....... } // Write the callback function print_result_cb. Data is the fourth parameter in sqlite3_exec, the second parameter is the number of columns, and the third parameter is the column name, the fourth is the query result. These two functions output all the query results.
Int print_result_cb (void * data, int n_columns, char ** column_values, Char ** column_names) { Static int column_names_printed = 0; Int I; If (! Column_names_printed ){ Print_row (n_columns, column_names ); Column_names_printed = 1; } Print_row (n_columns, column_values ); Return 0; } Void print_row (INT n_values, char ** values) { Int I; For (I = 0; I <n_values; ++ I ){ If (I> 0 ){ Printf ("\ t "); } Printf ("% s", values [I]); } Printf ("\ n "); } |
Void print_row (int n_values, char ** values)
{
Int I;
For (I = 0; I <n_values; ++ I ){
If (I> 0 ){
Printf ("\ t ");
}
Printf ("% s", values [I]);
}
Printf ("\ n ");
}
Void print_row (int n_values, char ** values) { Int I; For (I = 0; I <n_values; ++ I ){ If (I> 0 ){ Printf ("\ t "); } Printf ("% s", values [I]); } Printf ("\ n "); } This is the general process. For details, refer to the sqlite api function description. For details, see www.sqlite.org. Char value [500]; // Define a parameter SQL command, where chn and eng are the data to be inserted. Sprintf (value, "insert into chn_to_eng (chinese, english) VALUES ('% s',' % s')", chn, eng );
// Use the sqlite c/C ++ API to create and adjust a database. Rc = sqlite3_exec (pDB, Value, 0, 0, & errMsg ); // Query a record Char value [500]; // Define a query statement. The condition is the Chinese record when the english is target. // Print_result_cb is the callback function. You can obtain the query result. See the following for details. Sprintf (value, "SELECT chinese FROM eng_to_chn where english = '% S'", target ); Rc = sqlite3_exec (pDB, Value, Print_result_cb, 0, & errMsg ); If (rc = SQLITE_ OK) { // # Ifdef_debug Cout <"select the record successful! "<Endl; // # Endif } Else { // # Ifdef_debug Cout <errMsg <endl; // # Endif Return false; } ....... } // Write the callback function print_result_cb. data is the fourth parameter in sqlite3_exec, the second parameter is the number of columns, and the third parameter is the column name, the fourth is the query result. These two functions output all the query results.
Int print_result_cb (void * data, int n_columns, char ** column_values, Char ** column_names) { Static int column_names_printed = 0; Int I; If (! Column_names_printed ){ Print_row (n_columns, column_names ); Column_names_printed = 1; } Print_row (n_columns, column_values ); Return 0; } Void print_row (int n_values, char ** values) { Int I; For (I = 0; I <n_values; ++ I ){ If (I> 0 ){ Printf ("\ t "); } Printf ("% s", values [I]); } Printf ("\ n "); } |
This is the general process. For details, refer to the sqlite api function description. For details, see www.sqlite.org.