SQLite database is 0 configured, SQLite database is different from SQL Server and other databases, SQLite does not require complex configuration, only need to copy SQLite library files and dynamic link files to the appropriate project directory, you can use the SQLite database, Just use the database basic operation of the students, recommended to choose SQLite Database, for everyone to introduce the use of SQLite database API functions to achieve some basic operations.
SQLite application and embedded, should be small and convenient, and no other database complex configuration, we write some small programs or database is not very complex operation, SQLite is a good choice.
We use SQLite when we need to download its compressed package, there are many online,
After extracting, we can see that there are three files in the sqlite3.h and Sqlite3.lib and Sqlite3.dll that we need, and then we need to configure our compilers (vc6.0 or VS, the same steps)
(1) First, open the VS compiler, create a new WIN32 console program, F7 compile, run (of course nothing, to get the debug file)
(2) We then copied the downloaded Sqlite3.h,sqlite3.lib to the new Testsqlite directory. Then copy the sqlite3.h to the debug directory. In fact, just copy these files to the program directory, then we can add the path in the include, also can be found.
The above method of loading the library file, there are some drawbacks (in case we want to refer to a lot of files, are copied to the program directory, it will be cumbersome), the second method is to use the VS2010 compiler with the function: we click on the "Project", "Properties", the linker, Add the library file (Lib) that we used in the additional inclusion directory (the input header file or the file directory containing the file ), and then add the name of the library file (the file directory where the library file is located) in the additional library directory. Note that when adding a comma separated by commas ( at this point we still have to copy the dynamic link file to the program directory ), so we have finished loading the library file. Here's how:
(3)
(4) Additional Include Directories
(5) Additional dependencies
The above methods of using third-party libraries see how to use third-party libraries http://blog.csdn.net/u014028070/article/details/42278007
The above method is quite similar to what we have to include in the library file in order to invoke. We then add the following header file to the main program's header file (the header file where SQLite's API functions are located). Then we compile and check that the previous operation is correct.
At this point we use the SQLITEAPI function in the program to manipulate the database. The first is to create a database, at which point we define a SQLite action handle (which is executed by the handle for later opening, inserting, executing, etc.), and then defining a character pointer, array, and pointer to pass the error message, which is used to store our SQL statements.
The Sqlite3_open () function is used to open a database, if it exists, it is opened directly, if it does not exist, then establish one.
Run a bit.
At this point we enter the directory of the program, we will find a suffix named db file, this is the database we created, right-click Properties, see the size of 0kb (because there is no data inserted).
At this point, we do the insert data operation, the insert operation first to establish the data to belong to the property (relational database concept), we add three properties in the database, is sno,sname,sex. They are shaped, animated, and character-based.
<span style= "FONT-SIZE:18PX;" > sqlite3 * conn = NULL; char * err_msg = Null;char sql[200] = "" ;//Open database, create connection if (Sqlite3_open ("Student.db", &conn)! = SQLITE_OK) {cout<< "Can't open it! ";} CREATE TABLE student You cannot create a table again after creating a sprintf (SQL, "CREATE TABLE student_for_table (sno int, sname varchar (), age int)"); QLITE3_EXEC (conn, SQL, NULL, NULL, &ERR_MSG)! = SQLITE_OK) {cout<< "operation failed with error code:%s" << err_msg; exit (-1);} </span>
Then we perform the insert operation. (This is just a demo of inserting data)
<span style= "FONT-SIZE:18PX;" >int Main (int argc, _tchar* argv[]) {SQLITE3 * conn = NULL; char * err_msg = Null;char sql[200] = "" ;//Open database, create connection if (Sqlite3_open ("student.db", &conn)! = SQLITE_OK) {cout<< "Can't open! ";} CREATE TABLE student You cannot create a table again after creating a sprintf (SQL, "CREATE TABLE student_for_table (sno int, sname varchar (), age int)"); QLITE3_EXEC (conn, SQL, NULL, NULL, &ERR_MSG)! = SQLITE_OK) {cout<< "operation failed with error code:%s" << err_msg; exit (-1);} / /table creation completed, insert data for (int i = 0; i < i++) {//execute sqlsprintf (SQL, "INSERT INTO student_for_table (Sno, sname, age) VALUES (%d, '%s ',%d) ", I," students ", I); if (SQLITE3_EXEC (conn, SQL, NULL, NULL, &ERR_MSG)! = SQLITE_OK) { C out<< "operation failed with error code:%s" << err_msg; Exit (-1); }}</span>
<span style= "FONT-SIZE:18PX;" >}</span>
Then execute, we go into the program directory, and then we can see that the size of the database is not empty.
After inserting the data, of course we need to read it and then display it, so add the following code.
//read out the data, display it, Sqlite3_exec () Execute, invoke callback function, display operation written in backfill function
<span style= "FONT-SIZE:18PX;" >sprintf (SQL, "select * from student_for_table"); SQLITE3_EXEC (conn, SQL, &sqlite3_exec_callback, 0, &err_ msg);</span>
To read the data, we define a callback function:
<span style= "FONT-SIZE:18PX;" >//for data readout, use the callback function int sqlite3_exec_callback (void *data, int ncolumn, char **colvalues, char **colnames) {for (int i = 0; I < 3; i++) {printf ("%s\t", Colvalues[i]);} printf ("\ n"); return 0;} </span>
The final procedure is as follows:
<span style= "FONT-SIZE:18PX;" >//TestSqlite.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include "sqlite3.h" using namespace std; #pragma comment (Lib, " Sqlite3.lib ")//For data readout, use callback function int sqlite3_exec_callback (void *data, int ncolumn, char **colvalues, char **colnames) {for (int i = 0; i < 3; i++) {printf ("%s\t", Colvalues[i]);} printf ("\ n"); return 0;} int main (int argc, _tchar* argv[]) {SQLITE3 * conn = NULL; char * err_msg = Null;char sql[200] = "";//Open database, create connection if (sqli Te3_open ("Student.db", &conn)! = SQLITE_OK) {cout<< "Can't open! ";} CREATE TABLE Student CREATE table cannot be created again after sprintf (SQL, "CREATE TABLE student_for_table (sno int, sname varchar (), age int)"); if (SQLITE3_EXEC (conn, SQL, NULL, NULL, &ERR_MSG)! = SQLITE_OK) {cout<< ' operation failed with error code: '%s ' << err_msg; exit (-1 ); }//Table creation completed, insert data for (int i = 0; i < i++) {//execute sqlsprintf (SQL, "INSERT into student_for_table (Sno, sname, age) V Alues (%d, '%s ',%d) ", I," students ", I); if (SQLITE3_EXEC (conn, SQL, NULL, NULL, &err_msg)! = SQLITE_OK) {cout<< "operation failed with error code:%s" << err_msg; Exit (-1); The}//reads the data, displays it, Sqlite3_exec () executes, invokes the callback function, and the displayed operation is written in the backfill function sprintf (SQL, "select * from student_for_table"); Sqlite3_exec ( conn, SQL, &sqlite3_exec_callback, 0, &err_msg);//After you have finished working with the database, be sure to close the connection. if (SQLITE3_CLOSE (conn)! = SQLITE_OK) {cout<< "cannot be closed, error code:%s\n" << sqlite3_errmsg (conn) <<endl;; Exit (-1);} cout<< "Operation succeeded" <<endl;return 0;} </span>
The results of the operation are as follows:
The above shows how C + + is connected to SQLite, and we can also use MFC together with SQLite to make the operation with the interface, the core of the idea is consistent, just put the operation of the SQLite database in a different response function (MFC message), such as we put " Open database "Action is placed in the response function of a button. This allows us to make a database operation program with an interface. This is just a simple operation, and a deeper effort is needed to make something worthwhile.
SQLite connects to C + +