由于项目中要使用到SQLite数据库,所以特地学习记录下来
首先
先去官网下载源代码
https://www.sqlite.org/download.html
我下的是这个:
然后将解压出来的四个文件新建VS项目编译成lib文件
然后将lib文件和放入到要操作SQLite 的vs工程下
加入如下代码:
#include "sqlite3.h"
#pragma comment( lib,"sqlite3.lib" )
然后就可以开始增删改查了
SQLite C/C++ Tutorial
C/C++ Interface APIs
介绍三个主要的 API,知道这三个API就可以简单的使用sqlite3了。tutotial写得非常清楚,我直接复制过来了
1.sqlite3_open(const char *filename, sqlite3 **ppDb)
This routine opens a connection to an SQLite database file and returns a database connection object to be used by other SQLite routines.
If the filename argument is NULL or ‘:memory:’, sqlite3_open() will create an in-memory database in RAM that lasts only for the duration of the session.
If filename is not NULL, sqlite3_open() attempts to open the database file by using its value. If no file by that name exists, sqlite3_open() will open a new database file by that name.
2.sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg)
This routine provides a quick, easy way to execute SQL commands provided by sql argument which can consist of more than one SQL command.
Here, first argument sqlite3 is open database object, sqlite_callback is a call back for which data is the 1st argument and errmsg will be return to capture any error raised by the routine.
3.sqlite3_close(sqlite3*)
This routine closes a database connection previously opened by a call to sqlite3_open(). All prepared statements associated with the connection should be finalized prior to closing the connection.
If any queries remain that have not been finalized, sqlite3_close() will return SQLITE_BUSY with the error message Unable to close due to unfinalized statements.
更多使用例子:
https://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm