0

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:

  1. Learn the C++ implementation straight up, and use it like that.
  2. Find an existing wrapper for SQLite in C++, as it will save me from headaches.

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).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
muttley91
  • 12,278
  • 33
  • 106
  • 160
  • 1
    Try [this post at StackOverflow](http://stackoverflow.com/questions/120295/what-is-a-good-oo-c-wrapper-for-sqlite). I've been using sqlite directly, it's easy enough after reading a few tutorials and docu – makciook Jan 30 '13 at 19:32
  • I just called the c functions directly. They're well documented and work just fine. – Jay Jan 30 '13 at 21:41
  • Could you provide a tutorial that could help me get on the right track? – muttley91 Jan 31 '13 at 04:30

1 Answers1

1

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"))));

Filip
  • 155
  • 10