Post address: http://blog.csdn.net/tipfoo/article/details/2003220
System Environment: XP + VC6.0 sp6
SQLite is a database suitable for embedded applications. It is small, fast, and reliable. Real open-source resources are free of charge and do not retain any copyright (Public Domain ). It does not need to run additional server processes. It seems more concise than MS Access to use it to develop desktop database applications.
[1. Generate the SQLite library file]
Create the "C:/mylibs/libSQLite3" directory and use it as our current working directory.
(1) download SQLite 3.5.4 of Windows on the official website of SQLite ,:
Http://www.sqlite.org/sqlitedll-3_5_4.zip
Decompress the "sqlite3.def" and "sqlite3.dll" files to "C:/mylibs/libSQLite3 /";
Open the "command prompt" window and use the LIB command to generate the lib file used for connection:
Cd c:/mylibs/libSQLite3
LIB/DEF: sqlite3.def/machine: IX86
The "sqlite3.lib" and "sqlite3.exp" files are generated.
(2) download the SQLite3 source code from the official SQLite Website ,:
Http://www.sqlite.org/sqlite-source-3_5_4.zip
Decompress the "sqlite3.h" file to "C:/mylibs/libSQLite3 /".
(3) Check the file list. The "C:/mylibs/libSQLite3/" Directory should contain the following five files:
Sqlite3.def
Sqlite3.dll
Sqlite3.exp
Sqlite3.h
Sqlite3.lib
[2. Compile the sample program]
(1) Open VC6.0 and create an empty "Win32 console application" project named "sqlitedemo" under the "D:/VCStudio/sqlitedemo" directory.
(2) Project → Settings. On the Link tab, select "General" for "Category" and enter "sqlite3.lib" at the end of "Object/library modules". Note that each item is separated by spaces.
(3) copy the "libSQLite3" directory and its files to our project directory. Then, move the "sqlite3.lib" and "sqlite3.dll" files under the "libSQLite3" directory to the project directory.
In this way, add a line to the top of the CPP file that requires the sqlite Library:
# Include "sqlite3.h"
Then, you can call all functions in sqlite3.dll in the file.
(4) create a sample database. Open the "command prompt" window and switch to the project directory. Run:
D:/VCStudio/sqlitedemo> sqlite3 app. db
SQLite version 3.5.4
Enter ". help" for instructions
Sqlite> create table t1 (c1 TEXT );
Sqlite> insert into t1 VALUES ('Hello World! ');
Sqlite> SELECT * FROM t1;
Hello World!
Sqlite>. exit
(5) create a "C ++ source file" named "sqlitedemo. cpp", select "add Project", and write the code.
# Include <stdio. h>
# Include <stdlib. h>
# Include "./libSQLite3/sqlite3.h"
Static int _ callback_exec (void * notused, int argc, char ** argv, char ** aszColName)
{
Int I;
For (I = 0; I <argc; I ++)
{
Printf ("% s = % s/n", aszColName [I], argv [I] = 0? "NUL": argv [I]);
}
Return 0;
}
Int main (int argc, char * argv [])
{
Const char * sSQL = "select * from t1 ;";
Char * pErrMsg = 0;
Int ret = 0;
Sqlite3 * db = 0;
Ret = sqlite3_open ("./app. db", & db );
If (ret! = SQLITE_ OK)
{
Fprintf (stderr, "cocould not open database: % s", sqlite3_errmsg (db ));
Exit (1 );
}
Printf ("Successfully connected to database/n ");
Sqlite3_exec (db, sSQL, _ callback_exec, 0, & pErrMsg );
If (ret! = SQLITE_ OK)
{
Fprintf (stderr, "SQL error: % s/n", pErrMsg );
Sqlite3_free (pErrMsg );
}
Sqlite3_close (db );
Db = 0;
Return 0;
}
Press Ctrl + F5. The result is as follows:
Successfully connected to database
C1 = Hello World!
Press any key to continue