What is a good OO C++ wrapper for sqlite

C++DatabaseSqlite

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

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 - C++

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 3 - C++

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 4 - C++

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 - C++

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

Solution 6 - C++

I also wasn't pleased with what I could find. Now you can write:

class Person {
public:
	Person() {}
	static SqlTable<Person>& table() {
		static SqlTable<Person> tab = SqlTable<Person>::sqlTable("Person",
			SqlColumn<Person>("Firstname",  makeAttr(&Person::firstname)),
			SqlColumn<Person>("Lastname", 	makeAttr(&Person::lastname)),
			SqlColumn<Person>("Age", 		makeAttr(&Person::age)),
		return tab;
	}
	std::string firstname;
	std::string lastname;
	int age;
};

SqliteDB db("testtable.db");
auto sel(db.select<Person>("Firstname=\"Danny\" and Lastname=\"Zeckzer\""));
std::for_each(sel.first, sel.second, [](const Person& p) {
...
Person me;
db.insert<Person>(me);
...
std::vector<Person> everybody;
db.insert<Person>(everybody.begin(), everybody.end());

The table method is all you need to write as long as you stick to the sqlite3 data types. As everything is a template not much abstraction layer code remains after -O. Natural joins require a result class similar to the Person class. The implementation is a single header with less than 500 lines. License is LGPL. Source

Solution 7 - C++

Everyone have given good advice on what to use: I'll tell you what instrument NOT use.

LiteSQL.

My experience is terrible.
I'm just doing some reasearch on what orm use, and I'm testing a lot of it.

Weaknesses:

  • no documentation

  • no explanatory README

  • no explanation on prerequisites

  • do not compile due to a lot of bug (isn't true, isn't fixed in v0.3.17)

Solution 8 - C++

I wasn't pleased with any I could find either, so I wrote my own: sqlite3cc.

Here's a code example:

sqlite::connection db( filename );

sqlite::command c( db, "UPDATE foo SET bar = ? WHERE name = ?" );
c << 123 << name << sqlite::exec;

sqlite::query q( db, "SELECT foo FROM bar" );
for( sqlite::query::iterator i = q.begin(); i != q.end(); i++ )
	std::cout << i->column< std::string >( 0 ) << "\n";

Solution 9 - C++

I've used this one http://www.codeproject.com/KB/database/CppSQLite.aspx but I've moved to C#, so there may be newer/better ones now

Solution 10 - C++

http://www.codeproject.com/KB/database/CppSQLite.aspx is just fantastic, it is very easy to port, I had it working on bcb5 (omg) in half an hour or so. It is about as thin as you can get and easy to understand. There are a goodly number of examples that cover just about every thing you need to know. It uses exceptions for error handling - I modified it to provide return codes in a mater of minutes. Only tricky issue is to create your own lib file none are provided.

try
{

    CppSQLite3DB db;

    db.open(asFileName.c_str());

    db.execDML("Update data set hrx = 0");
   
} // try

catch (...)
{

} // catch

Could not be much simpler than this.....

Solution 11 - C++

Another simple one is NLDatabase. Disclaimer: I'm the author. Basic usage (and to be honest, you won't get much more than "basic" from this one) looks like this:

#include "NLDatabase.h"


using namespace std;
using namespace NL::DB;


int main(int argc, const char * argv[]) {

    Database db( "test.sqlite" );

    auto results = db.query("SELECT * FROM test WHERE name <> ?").select("TOM");

    for ( auto const & row : results ) {
        cout << "column[0]=" << row.column_string( 0 ) << endl;
    }
}

And just for fun, open a database, run a query and fetch results all in one line:

for ( auto & row : Database( "test.sqlite" ).query( "SELECT * FROM test").select() ) {
    cout << row.column_string( 0 ) << endl;
}

Solution 12 - C++

Perhaps you can take a look at

http://pocoproject.org

or

Platinum C++ Framework

Solution 13 - C++

I made one because of the need in our company. https://www.github.com/rubdos/libsqlitepp It's C++11, and header only. Just put the header in your project, include it and link to the C sqlite libraries.

Examples should be somewhere on that git repo too, fairly easy to use.

Solution 14 - C++

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFoo42View Question on Stackoverflow
Solution 1 - C++Johan KotlinskiView Answer on Stackoverflow
Solution 2 - C++jk.View Answer on Stackoverflow
Solution 3 - C++aminView Answer on Stackoverflow
Solution 4 - C++markshizView Answer on Stackoverflow
Solution 5 - C++zaharpopovView Answer on Stackoverflow
Solution 6 - C++burnerView Answer on Stackoverflow
Solution 7 - C++Luca DavanzoView Answer on Stackoverflow
Solution 8 - C++edamView Answer on Stackoverflow
Solution 9 - C++Brad BruceView Answer on Stackoverflow
Solution 10 - C++David CameronView Answer on Stackoverflow
Solution 11 - C++Tomas AndrleView Answer on Stackoverflow
Solution 12 - C++SebastianKView Answer on Stackoverflow
Solution 13 - C++rubdosView Answer on Stackoverflow
Solution 14 - C++Chris KView Answer on Stackoverflow