SQLite简介
SQLite是一款开源的、嵌入式关系型数据库。
简单地说,SQLite源代码只有三个文件,添加到项目中(此为嵌入式),通过调用SQLite提供的接口函数即可获得使用SQL语句读写数据库的能力,只是该数据库是内存数据库或本地文件数据库。
使用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <sqlite3.h> #include <iostream> using namespace std; int main(int argc, char **argv) { // sqlite API大多数函数所需的字符串均为utf8字符串 // 实际使用时需要考虑字符集转换 // 以下示例中省略错误处理代码 // 打开数据库 sqlite3* db; sqlite3_open("foods.db", &db); // 编译查询语句 sqlite3_stmt* stmt = nullptr; const char* tail = nullptr; char* sql = "select * from episodes"; sqlite3_prepare_v2(db, sql, -1, &stmt, &tail); // 执行查询 int rc = sqlite3_step(stmt); int columnCount = sqlite3_column_count(stmt); while (rc == SQLITE_ROW) { for (int i=0; i<columnCount; ++i) cout << sqlite3_column_text(stmt, i) << ' '; cout << endl; rc = sqlite3_step(stmt); } sqlite3_finalize(stmt); sqlite3_close(db); return 0; } |
推荐教程
《SQLite权威教程》 Grant Allen