Using sqlite in c + + under windows

Posted by KDragon on Wed, 02 Feb 2022 22:24:47 +0100

Using git warehouse

git clone https://github.com/sqlite/sqlite sqlite/sqlite
cd sqlite\sqlite
git checkout version-3.10.0 -b version-3.10.0

Tcl85 is required to build sqlite3, ActiveTcl
Download ActiveTcl 8.5 and compile it with MSVC after installation. I use vs2015

# Command under cmd
cd sqlite\sqlite
cmd /k D:\VS2015\VC\vcvarsall.bat :: Enable MSVC Compiler environment for
mkdir bld
cd bld
nmake /f ..\Makefile.msc ^  :: '^' It means to continue typing on a new line, '::' Is a line comment
TOP=..\ ^ :: TOP This is the directory that contains this "Makefile.msc".
TCLLIBDIR=D:\Tcl\lib ^
TCLINCDIR=D:\Tcl\include ^
LIBTCLPATH=D:\Tcl\bin 
:: sqlite3.10.0 The default is thread safe. If not,
:: stay Makefile.msc Found in-DSQLITE_THREADSAFE=1
:: Change to-DSQLITE_THREADSAFE=0

You can now find it in the bld directory
sqlite3.h
sqlite3.c
sqlite3.lib
sqlite3.dll
sqlite3.def

  • The code can be used directly with SQLite3 h sqlite3. c. But it's not good for old compilers
  • The static library uses SQLite3 h sqlite3. lib
  • The dynamic library uses SQLite3 h sqlite3. lib sqlite3. dll
:: without lib and exp Files can be generated using the following instructions
LIB /MACHINE:IX86 /DEF:sqlite3.def

Download and use the official website

Download the corresponding library, source code and tools

Dynamic library compilation:

ps: if there are no special requirements, you can directly use the downloaded SQLite dll - * * * Zip dll, if you don't trust, compile it yourself.
1. Use vs2010 to create a win32 project, and then select DLL and empty project. vc6-vs2015 is supported under normal circumstances
2. Put SQLite3 c,sqlite3.h,sqlite3ext.h,sqlite3. Copy def to the project source file directory. The first three files are in the first zip and the last two zip. Which one to use depends on your target environment.
3. Then add the above four files to the project through the resource manager of the project
4. Modify the project configuration and add in the configuration attribute – > C / C + ± - > preprocessor – > preprocessor definition
SQLITE_ENABLE_RTREE
SQLITE_ENABLE_COLUMN_METADATA
5. Modify the project configuration and add SQLite3 in the configuration attribute - > linker - > Input - > module definition file Def, and then compile it.

Static library compilation:

1. Use vs2010 to create a win32 project, then select the static library and remove the precompiled header. Under normal circumstances, vc6-vs2015 is supported
2. Put SQLite3 c,sqlite3.h,sqlite3ext.h,sqlite3. Copy def to the project source file directory. The first three files are in the first zip and the last two zip. Which one to use depends on your target environment.
3. Then add the above four files to the project through the resource manager of the project
4. Modify the project configuration and add in the configuration attribute – > C / C + ± - > preprocessor – > preprocessor definition
SQLITE_ENABLE_RTREE
SQLITE_ENABLE_COLUMN_METADATA
5. Modify the project configuration and add SQLite3 in the configuration attribute - > linker - > Input - > module definition file Def, and then compile it.

example

Simple use

#include "sqlite3.h"
#include <string>
#include <memory>
#include <stdio.h>

/* Prevent sql injection escape in sqlite */
static std::string sql_escape(const std::string& s){
	std::string res;
	for(const auto& c : s){
		switch(c){
		case '\'':
			res.push_back(c);
		default:
			res.push_back(c);	
		}
	}
	return res;
}
int main(int argc,char* argv[]){
	sqlite3* db=nullptr;
	int err = sqlite3_open(R"(.\test.db3)",&db);
	if(err){ return -1;}
	std::unique_ptr<sqlite3,std::function<void(sqlite3*)>> defer_(db,[](sqlite3* db){sqlite3_close(db);});
	char buff[2048];
	sprintf(buff,"select count(1) from table_1 where key='%s';",sql_escape("' 1=1; --").c_str());
	char *errmsg;
	bool is_had = false;
	err = sqlite3_exec(db,buff,[](void* selfdata,int ct,char** va,char** co)->int {
		if(ct <= 0){return -1;}
		bool &res = *((bool*)(selfdata));
		res = atoi(va[0]) > 0;
		return 0;
	},&is_had,&errmsg);
	if(err){
		sprintf(stderr,"error:%s\n",errmsg);
		sqlite3_free(errmsg);
	}
	printf("result: %i\n",is_had);
	return 0;
}

Topics: Windows SQLite window