文章目录
SQLite3 C++
#0 GitHub
example代码
#1 环境
macOS
C++14
#2 安装sqlite3
git clone https:
cd sqlite && mkdir bld && cd bld
../configure
make
make sqlite3.c
make test
sudo make install
#3 使用
#3.1 基本SQL语句
略
#3.2 sqlite3 API
- 打开数据库
int sqlite3_open_v2(
const char *filename,
sqlite3 **ppDb,
int flags,
const char *zVfs
);
flags:
flags说明SQLITE_OPEN_NOMUTEX设置数据库连接运行在多线程模式(没有指定单线程模式的情况下)SQLITE_OPEN_FULLMUTEX设置数据库连接运行在串行模式SQLITE_OPEN_SHAREDCACHE设置运行在共享缓存模式SQLITE_OPEN_PRIVATECACHE设置运行在非共享缓存模式SQLITE_OPEN_READWRITE指定数据库连接可以读写SQLITE_OPEN_CREATE如果数据库不存在,则创建……
返回值: 成功/失败
- 关闭数据库
int sqlite3_close_v2(sqlite3*)
- 句柄
sqlite3_stmt* stmt = nullptr;
- 校验SQL语句合法性
int sqlite3_prepare_v2(
sqlite3 *db,
const char *zSql,
int nByte,
sqlite3_stmt **ppStmt,
const char **pzTail
);
返回值: 合法/非法
- 执行
int sqlite3_step(sqlite3_stmt*)
返回值: 成功/失败
- 清理语句句柄
int sqlite3_finalize(sqlite3_stmt *pStmt)
返回值: 成功/失败
- SQL语句
const char* sql_sentence = "select name,age from Persons where age;
- 获取相应数据
API说明sqlite3_column_double浮点数据sqlite3_column_int整型数据sqlite3_column_int64长整型数据sqlite3_column_blob二进制文本数据sqlite3_column_text字符串数据
#3.3 Code
#include
#include
#include
class SQL3 {
public:
SQL3() {
int result = sqlite3_open_v2(m_path, &m_sql,
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_NOMUTEX|SQLITE_OPEN_SHAREDCACHE,
nullptr);
if (SQLITE_OK == result) {
std::cout << "SQLite3 打开成功" << std::endl;
} else {
std::cout << "SQLite3 打开失败" << std::endl;
}
}
~SQL3(){
sqlite3_close_v2(m_sql);
}
public:
void create_table() {
const char* sql_sentence = "CREATE TABLE IF NOT EXISTS [Persons] ([name] VARCHAR NOT NULL,[age] INT NULL);";
this->exec(sql_sentence);
}
void insert() {
const char* sql_sentence1 = "INSERT INTO Persons(name, age) VALUES('Trunk', 4);";
this->exec(sql_sentence1);
const char* sql_sentence2 = "INSERT INTO Persons(name, age) VALUES('Master', 6);";
this->exec(sql_sentence2);
}
void update() {
const char* sql_sentence = "UPDATE Persons set age=8 where name='Master'";
this->exec(sql_sentence);
}
void del() {
const char* sql_sentence = "delete from Persons where name='Master'";
this->exec(sql_sentence);
}
nlohmann::json get() {
nlohmann::json data;
const char* sql_sentence = "select name,age from Persons where age;
sqlite3_stmt* stmt = nullptr;
int result = sqlite3_prepare_v2(m_sql, sql_sentence, -1, &stmt, nullptr);
if (SQLITE_OK == result) {
while (SQLITE_ROW == sqlite3_step(stmt)) {
const unsigned char* name = sqlite3_column_text(stmt, 0);
int age = sqlite3_column_int(stmt, 1);
data[(char*)name] = age;
}
} else {
std::cout << "添加数据语句有问题" << std::endl;
}
sqlite3_finalize(stmt);
return data;
}
private:
int exec(const char* s) {
sqlite3_stmt* stmt = nullptr;
int result = sqlite3_prepare_v2(m_sql, s, -1, &stmt, nullptr);
if (SQLITE_OK == result) {
sqlite3_step(stmt);
} else {
std::cout << "添加数据语句有问题" << std::endl;
}
sqlite3_finalize(stmt);
return result;
}
sqlite3* m_sql = nullptr;
const char* m_path = "../test.db";
};
int main() {
std::cout << "Hello, SQLite3!" << std::endl;
SQL3 sql;
sql.create_table();
sql.insert();
sql.update();
std::cout << sql.get() << std::endl;
return 0;
}
Original: https://blog.csdn.net/Coxhuang/article/details/121361460
Author: autoooooooo
Title: SQLite3 Cpp基本使用
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/816137/
转载文章受原作者版权保护。转载请注明原作者出处!