【问题标题】:Using SQLite in C++ - Wrapper?在 C++ 中使用 SQLite - 包装器?
【发布时间】:2019-12-11 06:36:15
【问题描述】:

对于一个项目,我将使用 SQLite 和一些我们用 C++ 开发的软件。我在 PHP 中使用了一些 SQLite,但对于在 Web 开发之外使用数据库有点陌生。我想知道我是否应该:

  1. 直接学习 C++ 实现,然后像这样使用它。
  2. 在 C++ 中查找现有的 SQLite 包装器,因为它可以让我免于头痛。

我正在考虑使用包装器,因为在没有包装器的情况下使用 SQLite 的函数看起来可能有点混乱。为了干净的代码,我倾向于使用包装器。如果是这样,哪个包装器最干净且最常用?是否有开发人员使用的 SQLite 标准包装器?

否则,是否有在 C++ 中使用 SQLite 的好教程?我无法找到一套清晰的说明(是的,我查看了文档)。

【问题讨论】:

  • 试试this post at StackOverflow。我一直在直接使用sqlite,看了几篇教程和文档就很简单了
  • 我只是直接调用了c函数。它们有据可查,工作正常。
  • 你能提供一个教程来帮助我走上正轨吗?

标签: c++ sqlite


【解决方案1】:

如果你熟悉 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"))));

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2018-10-12
    • 2010-09-10
    • 2017-02-09
    • 2015-10-08
    • 1970-01-01
    相关资源
    最近更新 更多