Skip to content

Instantly share code, notes, and snippets.

@fundaev

fundaev/main.cpp Secret

Created February 8, 2021 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fundaev/74c9fdf34c2f321c7cbdec03fe6c9a39 to your computer and use it in GitHub Desktop.
Save fundaev/74c9fdf34c2f321c7cbdec03fe6c9a39 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "sqlite3.h"
void exec(sqlite3 *db, const char* sql)
{
int code = sqlite3_exec(db, sql, nullptr, nullptr, nullptr);
if (code != SQLITE_OK)
throw std::runtime_error("failed to exec query: " + std::string(sql) + " : " + std::string(sqlite3_errmsg(db)));
}
void test()
{
static const char* filename = "test.db";
static const char* sql_create_table =
"create table t1 (id integer primary key, name text, age integer)";
sqlite3 *db = nullptr;
sqlite3_open(filename, &db);
exec(db, sql_create_table);
exec(db, "savepoint sp1");
exec(db, "insert into t1 (name, age) values('item1', 1)");
exec(db, "rollback to sp1");
exec(db, "savepoint sp2");
exec(db, "insert into t1 (name, age) values('item2', 2)");
exec(db, "release sp2");
sqlite3_close(db);
}
int main(int, char**)
{
test();
std::cout << "[ ok ]" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment