The following steps will guide you through how you can connect to SQLite databases using C++.
1. Download and extract the following files into a single folder C:\SQLite. SQLite Source Code: sqlite-amalgamation-3071100.zip SQLite DLL for Windows: sqlite-dll-win32-x86-3071100.zip SQLite Shell for Windows: sqlite-shell-win32-x86-3071100.zip
2. Let’s create a SQLite database named “test.db” with a single table named “table1”. You might do this at a shell or DOS prompt:
Once you’ve done so, the test.db file will be created.
3. You will need to generate the sqlite3.lib file in this example program.
Run the command windows again with following commands.
Note: If you are using VS2005, please make sure that you copy the file named “mspdb80.dll” to the SQLite DLL folder. Otherwise, the LIB command will fail.
4. Create a new VC++ console application. Let’s called it “sqlite” project.
5. Make sure that you add the “C:\SQLite” file path into the project properties settings as following: [C/C++ -> General -> Additional Includes Directories] [Linker -> General -> Additional Library Directories]
6. Add “sqlite3.lib” to the following property setting: [Linker -> Input -> Additional Dependencies]
7. Let’s enter the following code into the sqlite project main function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // Make sure the you include the sqlite3 header file #include #include "sqlite3.h" int _tmain(int argc, char * argv[]) { sqlite3 *db; // sqlite3 db struct char *zErrMsg = 0; int rc; // Open the test.db file rc = sqlite3_open(argv[1], &db); if( rc ){ // failed fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); } else { // success fprintf(stderr, "Open database successfully\n"); } // Open the test.db file sqlite3_close(db); getchar(); return 0; } |
8. Compile the source code and it will generate an executable file. In this tutorial, an EXE named “sqlite.exe” will be generated.
9. Copy the following files to a new folder C:\SQL_Test.
10. Go to shell or DOS prompt to test the application:
Error fixes:
1. If you can not find a tools folder inside the common7 folder, make sure that you have visual studio on your system and not just visual C++ . Also, Even then if you can not find the vsvars.bat file I recommend searching the file and then noting its path and then using it. (Newer versions of VStudio can also make the lib files)
2. Once you have made the lib files and all and then when you try to build and run the program, you may get the error that “you do not have sqlite3.dll file on your computer” . For this make sure that you have copied the sqlite3.dll file in C:windowssystem or C:windowssystem32.
Now compile and then this should work.
thank you very much , it is really helpful