Windows 7 + msvc 2012 trial version
The reason for using SQLite is mainly because friends cannot install things on the company's computer. mysql-based programs that need to be installed and need to start background services will naturally not work, So SQLite.
1. First of all, it should be declared that there is no need to install it. Many of the items on the Internet are compilation and the methods for generating lib are completely bypassed (I think ). Directly add the official C and H file including in the project. I don't know how other people actually developed it. One thing I know is that the sqlite3 support of QT directly covers the source code, instead of using the library to link it.
2. Download the source code from the official website: http://www.sqlite.com/download.html:
Decompress the package as follows:
D: \ SQLite Development \ source code \ sqlite-amalgamation-3071700> dir volume in drive D is D volume serial number is D8DC-109A directory of D: \ SQLite Development \ source code \ sqlite-amalgamation-30717002013/05/22 <dir>. <dir> .. 99,401 shell. c2013/05/20 4,973,863 sqlite3.c2013/05/20 348,618 sqlite3.h2013/05/20 25,974 sqlite3ext. H 4 file (s) 5,447,856 bytes 2 Dir (s) 37,436,293,120 bytes free
3. Create a vs Empty Project in the path D: \ qt_proj \ sqlite_cons1. The code is in the path D: \ qt_proj \ sqlite_cons1 \ sqlite_cons1. No code is available yet, copy sqlite3.c and sqlite3.h in step 2, and add a main file: source. CPP.
4. Complete the program function, that is, the implementation code of source. cpp:
# 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 [I], argv [I]? Argv [I]: "null");} printf ("\ n"); Return 0;} int main () {sqlite3 * dB; char * zerrmsg = 0; int RC; rc = sqlite3_open (". /test. DB ", & dB); If (RC) {fprintf (stderr," can't open database: % s \ n ", sqlite3_errmsg (db); sqlite3_close (db ); return (1);} rc = sqlite3_exec (dB, "create table if not exists work (first integer primary key autoincrement, second text)", callback, 0, & zerrmsg ); if (RC! = Sqlite_ OK) {fprintf (stderr, "SQL error: % s \ n", zerrmsg); sqlite3_free (zerrmsg);} rc = sqlite3_exec (dB, "insert into work (second) values ('taobao') ", callback, 0, & zerrmsg); If (RC! = Sqlite_ OK) {fprintf (stderr, "SQL error: % s \ n", zerrmsg); sqlite3_free (zerrmsg);} rc = sqlite3_exec (dB, "insert into work (second) values ('asdf ') ", callback, 0, & zerrmsg); If (RC! = Sqlite_ OK) {fprintf (stderr, "SQL error: % s \ n", zerrmsg); sqlite3_free (zerrmsg);} sqlite3_close (db); Return 0 ;}
5. Run the program to generate test. DB:
6. In this case, use the SQLite shell tool to view the results:
D:\Qt_Proj\Sqlite_cons1\Sqlite_cons1>Sqlite3 test.dbSQLite version 3.7.17 2013-05-20 00:56:22Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> .tableworksqlite> select * from work;1|│╘╖╣╟δ░┤2|asdfsqlite>
The above garbled characters are displayed because I modified the font and display in cmd, so that Chinese characters cannot be displayed.
7. Next, one of the compiled files is CPP, while the sqlite3 implementation file is a. c file. It doesn't matter. There is such a section in sqlite3.h:
#ifndef _SQLITE3_H_#define _SQLITE3_H_#include <stdarg.h> /* Needed for the definition of va_list *//*** Make sure we can call this stuff from C++.*/#ifdef __cplusplusextern "C" {#endif
8. If the lib and DLL files must be used, you do not need to re-compile the. c file and perform the following operations:
E: \ SQLite Development \ Windows tools> Cd sqlite-dll-win32-x86-3071700E: \ SQLite Development \ Windows tools \ sqlite-dll-win32-x86-3071700> the volume in the Dir drive E is the serial number of the E volume is 4cd7-e1dc E: \ SQLite Development \ Windows tools \ sqlite-dll-win32-x86-3071700 directory <dir>. <dir> .. 4,385 sqlite3.def2013/05/20 620,718 sqlite3.dll 2 files 625,103 bytes 2 directories 41,283,182,592 available bytes E: \ SQLite Development \ Windows tools \ sqlite-dll-win32-x86-3071700> lib/DEF: sqlite3.defmicrosoft (r) library Manager version 11.00.60315.1copyright (c) Microsoft Corporation. all rights reserved. link: Warning lnk4068:/machine not specified; defaulting to x86 creating Library sqlite3.lib and object sqlite3.expe: \ SQLite Development \ Windows tools \ sqlite-dll-win32-x86-3071700> dir drive E in the volume is the serial number of the E volume is 4cd7-e1dc E: \ SQLite Development \ Windows tools \ sqlite-dll-win32-x86-3071700 directory <dir>. <dir> .. 4,385 sqlite3.def2013/05/20 620,718 sqlite3.dll2013/05/22 29,576 sqlite3.exp2013/05/22 49,522 sqlite3.lib 4 files 704,201 bytes 2 directories 41,283,092,480 available bytes
That is, perform the following operations under the DLL folder path to obtain the corresponding lib file:
lib /def:sqlite3.def
Note: The next easy-to-understand error point here is that Lib files are required for both dynamic and static link libraries, but the former is based on the Lib File Link, then, find the DLL file during running, and then directly link the Lib file to the executable file.