-
-
Save patrick-palka/ffd836d0294f71d183f4199d0e842186 to your computer and use it in GitHub Desktop.
SQLite wrapper example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SQLiteWrapper.h" | |
int main(void) { | |
static const char db_name[] = "example.db"; | |
using db = sqlite::Database<db_name>; | |
static const char create_table_query[] | |
= R"(create table if not exists users (first_name text, | |
last_name text, | |
age integer, | |
website text))"; | |
db::query<create_table_query>(); | |
static const char clear_table_query[] = R"(delete from users)"; | |
db::query<clear_table_query>(); | |
static const char insert_users_query[] | |
= R"(insert into users values (?1, ?2, ?3, ?4))"; | |
db::query<insert_users_query>("John", "Doe", 29, "google.com"); | |
db::query<insert_users_query>("Mary", "Smith", 28, std::nullopt); | |
db::query<insert_users_query>("James", "Smith", 20, "yahoo.com"); | |
static const char select_users_query[] | |
= R"(select first_name, last_name, age, website | |
from users where age = ?1 or substr(first_name, 1, 1) = ?2)"; | |
auto fetch_row = db::query<select_users_query>(29, "M"); | |
std::string_view first_name, last_name; | |
int age; | |
std::optional<std::string_view> website; | |
while (fetch_row(first_name, last_name, age, website)) { | |
std::cout << first_name << " " | |
<< last_name << ", " | |
<< age << ", " | |
<< (website ? *website : "<no website>") | |
<< std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: