如果你熟悉 ORM https://en.wikipedia.org/wiki/Object-relational_mapping
我会推荐这个库,它使用起来非常简单,我已经使用了一段时间了,它比较容易使用,你只需要 sqlite 头文件和 sqlite_orm 库头文件就可以使用它。
https://github.com/fnc12/sqlite_orm
这是该库的 User 和 UserType 表的直接示例:
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"))));