Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yhirose/cpp-sqlitelib
C++ SQLite wrapper library
https://github.com/yhirose/cpp-sqlitelib
cpp head-only sqlite
Last synced: about 2 months ago
JSON representation
C++ SQLite wrapper library
- Host: GitHub
- URL: https://github.com/yhirose/cpp-sqlitelib
- Owner: yhirose
- License: mit
- Created: 2013-06-20T17:16:25.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2022-07-12T19:39:30.000Z (over 2 years ago)
- Last Synced: 2024-11-01T22:43:00.973Z (about 2 months ago)
- Topics: cpp, head-only, sqlite
- Language: C
- Homepage:
- Size: 3.76 MB
- Stars: 51
- Watchers: 5
- Forks: 19
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesomecpp - cpp-sqlitelib - - C++11 SQLite wrapper library. (Database)
README
cpp-sqlitelib
=============A single file C++ header-only SQLite wrapper library
## Open database
#include
using namespace sqlitelib;
auto db = Sqlite("./test.db");## Create table
db.execute(R"(
CREATE TABLE IF NOT EXISTS people (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
age INTEGER,
data BLOB
)
)");## Drop table
db.execute("DROP TABLE IF EXISTS people");
## Insert records
auto stmt = db.prepare("INSERT INTO people (name, age, data) VALUES (?, ?, ?)");
stmt.execute("john", 10, vector({ 'A', 'B', 'C', 'D' }));
stmt.execute("paul", 20, vector({ 'E', 'B', 'G', 'H' }));
stmt.execute("mark", 15, vector({ 'I', 'J', 'K', 'L' }));
stmt.execute("luke", 25, vector({ 'M', 'N', 'O', 'P' }));## Select a record (single colum)
auto val = db.execute_value("SELECT age FROM people WHERE name='john'");
val; // 10## Select records (multiple columns)
auto rows = db.execute("SELECT age, name FROM people");
rows.size(); // 4auto [age, name] = rows[3]; // age: 25, name: luke
## Bind #1
auto stmt = db.prepare("SELECT name FROM people WHERE age > ?");
auto rows = stmt.execute(10);
rows.size(); // 3
rows[0]; // paulauto rows = stmt.bind(10).execute();
rows.size(); // 3
rows[0]; // paul## Bind #2
auto val = db.execute_value("SELECT id FROM people WHERE name=? AND age=?", "john", 10);
val; // 1## Cursor (multiple columns)
auto stmt = db.prepare("SELECT name, age FROM people");
auto cursor = stmt.execute_cursor();
for (auto it = cursor.begin(); it != cursor.end(); ++it) {
get<0>(*it);
get<1>(*it);
++it;
}// With C++17 structured binding
for (const auto& [name, age] : stmt.execute_cursor()) {
;
}## Cursor (single column)
auto stmt = db.prepare("SELECT name FROM people");
for (const auto& x: stmt.execute_cursor()) {
;
}## Count
auto val = db.execute_value("SELECT COUNT(*) FROM people");
val; // 4## Flat API
for (const auto& [name, age] :
db.execute_cursor("SELECT name, age FROM people")) {
;
}for (const auto& x: db.execute_cursor("SELECT name FROM people")) {
;
}License
-------MIT license (© 2021 Yuji Hirose)