对于一个项目,我将使用SQLite和一些我们用C ++开发的软件。我在PHP中使用过SQLite,但是在Web开发之外使用数据库我有点新鲜。我想知道我是否应该:
我正在考虑一个包装器,因为使用SQLite的功能看起来不像它们可能有点乱。为了清洁代码,我倾向于使用包装器。如果是这样,哪个包装是最干净和最常用的?是否有开发人员使用的SQLite标准包装器?
否则,是否有一个在C ++中使用SQLite的好教程?我找不到一套明确的指示(是的,我查看了文档)。
答案 0 :(得分:0)
如果您熟悉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"))));
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"))));