irpas技术客

SQLite3 Cpp基本使用_autoooooooo_cppsqlite3

未知 7668

文章目录 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; }


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #cppsqlite3 #sqlite3 #C0 #C #demo #Github1 #环境macOSC142