What is a good OO C++ wrapper for sqlite

33,295

Solution 1

This is really inviting down-votes, but here goes...

I use sqlite directly from C++, and don't see any value with an added C++ abstraction layer. It's quite good (and efficient) as is.

Solution 2

I read this post and tried some of the libraries mentioned in the answers ,
But none of them was easy enough for me ( i am a lazy programmer ! ).

So i wrote my own wrapper : sqlite modern cpp

database db("dbfile.db");
// executes the query and creates a 'user' table if not exists
db << "create table if not exists user ("
      "   age int,"
      "   name text,"
      "   weight real"
      ");";
// inserts a new user and binds the values to '?' marks
db << "insert into user (age,name,weight) values (?,?,?);"
        << 20
        << "bob"
        << 83.0;
// slects from table user on a condition ( age > 18 ) and executes 
// the lambda for every row returned .
db << "select age,name,weight from user where age > ? ;"
   << 18
   >> [&](int age, string name, double weight) {
       cout << age << ' ' << name << ' ' << weight << endl;
   };
// selects the count(*) of table user
int count = 0;
db << "select count(*) from user" >> count;

Have fun !

Solution 3

Another good wraper for databases in C++ is SOCI. It's not very OO, but the more Modern C++.

It supports Oracle, PostgreSQL and MySQL. A SQLite backend is in the CVS.

Solution 4

Here's one that hasn't been updated in a while, but compiles and runs on Mac OS GCC 4.3. It's also released under the MIT License, so you can use it in a commercial project, no problems. http://code.google.com/p/sqlite3pp/

The usage is boost-ified and very clean:

sqlite3pp::database db("test.db");
sqlite3pp::transaction xct(db);
{
    sqlite3pp::command cmd(db, "INSERT INTO contacts (name, phone) VALUES (:user, :phone)");
    cmd.bind(":user", "Mike");
    cmd.bind(":phone", "555-1234");
    cmd.execute();
}
xct.rollback();

See: http://code.google.com/p/sqlite3pp/wiki/UsagePage

Solution 5

Use Qt - it has great binding for SQLite that fits well into its overall design

Share:
33,295
Foo42
Author by

Foo42

i love the frontendz

Updated on July 08, 2022

Comments

  • Foo42
    Foo42 almost 2 years

    I'd like to find a good object oriented C++ (as opposed to C) wrapper for sqlite. What do people recommend? If you have several suggestions please put them in separate replies for voting purposes. Also, please indicate whether you have any experience of the wrapper you are suggesting and how you found it to use.

  • NeDark
    NeDark over 13 years
    Is it no longer available for download?
  • edam
    edam over 13 years
    Yes, it is available. There has just been no proper releases yet. You can get the source code from the bazaar repository here or here (you'll need to download the bazaar RCS tools from here if you don't have them) or you can view the source code on-line here
  • CashCow
    CashCow over 13 years
    Well you only got -4 for the two downvotes and I upvoted you. Using the C interface certainly is an option and likely to be the one we choose. Of course we may use a few light C++ wrappers, possibly boost::shared_ptr with custom deleters, and possibly exception to handle errors, but no real need for a huge API.
  • Johan Kotlinski
    Johan Kotlinski over 13 years
    @CashCow: Yes - thanks - I did just the same.
  • WhozCraig
    WhozCraig almost 10 years
    Likewise, in the end, this was the most sane alternative we had as well.