SQLite3 Cpp基本使用
2021/11/16 19:09:59
本文主要是介绍SQLite3 Cpp基本使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文章目录
- SQLite3 C++
- #0 GitHub
- #1 环境
- #2 安装sqlite3
- #3 使用
- #3.1 基本SQL语句
- #3.2 sqlite3 API
- #3.3 Code
SQLite3 C++
#0 GitHub
example代码
SQLite3 C++ Demo Github
#1 环境
macOS C++14
#2 安装sqlite3
git clone https://github.com/sqlite/sqlite.git 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, /* Database filename (UTF-8) */ sqlite3 **ppDb, /* OUT: SQLite db handle */ int flags, /* Flags */ const char *zVfs /* Name of VFS module to use */ );
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; // 执行stmt句柄 如果指令能查询到下一行数据,就会返回SQLITE_ROW; 如果指令(例如写入数据)不需要返还数据,就会返还SQLITE_DONE
- 校验SQL语句合法性
int sqlite3_prepare_v2( sqlite3 *db, /* Database handle */ const char *zSql, /* SQL statement, UTF-8 encoded */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const char **pzTail /* OUT: Pointer to unused portion of zSql */ );
返回值: 合法/非法
- 执行
int sqlite3_step(sqlite3_stmt*)
返回值: 成功/失败
- 清理语句句柄
int sqlite3_finalize(sqlite3_stmt *pStmt)
返回值: 成功/失败
- SQL语句
const char* sql_sentence = "select name,age from Persons where age<10";
- 获取相应数据
API | 说明 |
---|---|
sqlite3_column_double | 浮点数据 |
sqlite3_column_int | 整型数据 |
sqlite3_column_int64 | 长整型数据 |
sqlite3_column_blob | 二进制文本数据 |
sqlite3_column_text | 字符串数据 |
#3.3 Code
#include <iostream> #include <sqlite3.h> #include <nlohmann/json.hpp> class SQL3 { public: SQL3() { /* flags: SQLITE_OPEN_NOMUTEX: 设置数据库连接运行在多线程模式(没有指定单线程模式的情况下) SQLITE_OPEN_FULLMUTEX:设置数据库连接运行在串行模式。 SQLITE_OPEN_SHAREDCACHE:设置运行在共享缓存模式。 SQLITE_OPEN_PRIVATECACHE:设置运行在非共享缓存模式。 SQLITE_OPEN_READWRITE:指定数据库连接可以读写。 SQLITE_OPEN_CREATE:如果数据库不存在,则创建。 * */ 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<10"; 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; // 执行stmt句柄 如果指令能查询到下一行数据,就会返回SQLITE_ROW; 如果指令(例如写入数据)不需要返还数据,就会返还SQLITE_DONE int result = sqlite3_prepare_v2(m_sql, s, -1, &stmt, nullptr); // 检查SQL语句的合法性 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";//某个sql文件的路径 }; int main() { std::cout << "Hello, SQLite3!" << std::endl; SQL3 sql; sql.create_table(); sql.insert(); sql.update(); // sql.del(); std::cout << sql.get() << std::endl; return 0; }
这篇关于SQLite3 Cpp基本使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-29Elasticsearch慢查询日志配置
- 2024-05-29揭秘华为如此多成功项目的产品关键——Charter模板
- 2024-05-29海外IDC业务拓展的7大挑战
- 2024-05-29InLine Chat功能优化对标Github Copilot,CodeGeeX带来更高效、更直观的编程体验!
- 2024-05-29CodeGeeX 智能编程助手 6 项功能升级,在Visual Studio插件市场霸榜2周!
- 2024-05-29AutoMQ 生态集成 Apache Doris
- 2024-05-292024年IDC行业的深度挖掘:机遇、挑战与未来展望
- 2024-05-29五款扩展组件齐发 —— Volcano、Keda、Crane-scheduler 等,邀你体验
- 2024-05-29AutoMQ 对象存储数据高效组织的秘密: Compaction
- 2024-05-29活动预告|来 GIAC 大会听大数据降本利器:AutoMQ 基于云原生重新设计的 Kafka