Reputation: 12684
For a project, I'm going to be using SQLite with some software we're developing in C++. I've been using SQLite for a bit in PHP, but I'm a bit new to using databases outside of web development. I'm wondering if I should:
I'm considering a wrapper because the functions for using SQLite without one look like they could be a bit messy. In the interest of clean code, I'm leaning towards using a wrapper. If so, which wrapper is the cleanest and most used? Is there a standard wrapper for SQLite that developers use?
Otherwise, is there a good tutorial for using SQLite in C++? I haven't been able to find a clear set of instructions (yes, I've looked at the documentation).
Upvotes: 0
Views: 1182
Reputation: 165
If you are familiar with ORM https://en.wikipedia.org/wiki/Object-relational_mapping
I would recommend this library, it is pretty straigh forward to use, i have been using it for a while now and it is relatively easy to use, you only need the sqlite header file and the sqlite_orm library header file to use it.
https://github.com/fnc12/sqlite_orm
Here is a straight forward example of User and UserType tables for the library:
struct User{
int id;
std::string firstName;
std::string lastName;
int birthDate;
std::unique_ptr<std::string> imageUrl;
int typeId;
};
struct UserType {
int id;
std::string name;
};
using namespace sqlite_orm;
auto storage = make_storage("db.sqlite",
make_table("users",
make_column("id", &User::id, autoincrement(), primary_key()),
make_column("first_name", &User::firstName),
make_column("last_name", &User::lastName),
make_column("birth_date", &User::birthDate),
make_column("image_url", &User::imageUrl),
make_column("type_id", &User::typeId)),
make_table("user_types",
make_column("id", &UserType::id, autoincrement(), primary_key()),
make_column("name", &UserType::name, default_value("name_placeholder"))));
Upvotes: 1