Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good OO C++ wrapper for sqlite [closed]

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.

like image 411
Foo42 Avatar asked Sep 23 '08 10:09

Foo42


People also ask

What is SQLite wrapper?

This source code provide the simple methods to create database and run queries in portable database. Created SQLite database file is platform independent and can be use or replicate to Windows or Linux OS.

How much load can SQLite handle?

SQLite supports databases up to 281 terabytes in size, assuming you can find a disk drive and filesystem that will support 281-terabyte files.

How do I close SQLite?

Ctrl + D will get you out of the SQLite 3 database command prompt. That is: hold the "Ctrl" button then press the lowercase d key on your keyboard at the same time and you will escape the SQLite 3 command prompt.

Is SQLite a good choice?

Embedded applications: SQLite is a great choice of database for applications that need portability and don't require future expansion. Examples include single-user local applications, mobile applications, or games.


8 Answers

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.

like image 147
Johan Kotlinski Avatar answered Sep 20 '22 18:09

Johan Kotlinski


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.

like image 44
jk. Avatar answered Sep 24 '22 18:09

jk.


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

like image 40
markshiz Avatar answered Sep 22 '22 18:09

markshiz


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 !

like image 23
amin Avatar answered Sep 21 '22 18:09

amin


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

like image 40
zaharpopov Avatar answered Sep 23 '22 18:09

zaharpopov


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

like image 40
burner Avatar answered Sep 21 '22 18:09

burner


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)
like image 40
Luca Davanzo Avatar answered Sep 23 '22 18:09

Luca Davanzo


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";
like image 39
edam Avatar answered Sep 20 '22 18:09

edam