SQLite C++ Wrapper

A minimalistic wrapper for SQLite 3.x, inspired by the ADO.NET interfaces.

Download

sqlite3x.zip sqlite3x source code

Open Source

SQLite C++ Wrapper is Open Source under the zlib License. It makes use of the following Open Source software: SQLite.

Development

Want the latest (possibly unstable) source code? GameStat is available from CVS:

cvs -d:pserver:anonymous@cvs.int64.org:/cvsroot/int64 login
cvs -d:pserver:anonymous@cvs.int64.org:/cvsroot/int64 co -P sqlite3x

Examples

Inserting data

#include <string>
#include <iostream>
#include <stdexcept>
using namespace std;

#include "sqlite3x.hpp"
using namespace sqlite3x;

int main(void) {
   try {
      sqlite3_connection con("test.db");

      int count = con.executeint(
      	"select count(*) "
      	"from sqlite_master "
      	"where name='t_test';");

      if(count == 0) {
         con.executenonquery(
            "create table t_test(number,string);");
      }

      sqlite3_transaction trans(con);
      {
         sqlite3_command cmd(con,
            "insert into t_test values(?,?);");
         cmd.bind(2, "foobar", 6);

         for(int i = 0; i < 10000; ++i) {
            cmd.bind(1, i);
            cmd.executenonquery();
         }
      }

      // if trans goes out of scope (due to an exception or
      // anything else) before calling commit(), it will
      // automatically rollback()
      trans.commit();
   }
   catch(exception &ex) {
      cerr << "Exception Occured: " << ex.what() << endl;
   }

   return 0;
}

Selecting data

#include <iostream>
#include <stdexcept>
using namespace std;

#include "sqlite3x.hpp"
using namespace sqlite3x;

int main(void) {
   try {
      sqlite3_connection con("test.db");

       sqlite3_command cmd(con, "select * from t_test;");
       sqlite3_reader reader = cmd.executereader();

       while(reader.read()) {
          cout << reader.getcolname(0) << ": "
               << reader.getint(0) << endl;
       }
   }
   catch(exception &ex) {
      cerr << "Exception Occured: " << ex.what() << endl;
   }

   return 0;
}