SQLite with C++ Example: A Compact Serverless Database Alternative

/* Posted December 9th, 2008 at 10:26pm */
/* Filed under C/C++, Programming */

/* */

sqlite1 SQLite with C++ Example: A Compact Serverless Database Alternative

Have you got an application you’re working on that requires the power to store and query data of a SQL database but don’t need the hassle of setting up a client and server solution? Today, let us introduce you to SQLite which was made just for you. Besides being complete free, it’s a small and fast library that acts as a SQL database wrapper to a plain text file that sits locally in your application. What this means is you can create and enforce all the great structured tables in your database design as well as construct intricate data queries just like you do with any server-backed SQL database, except your database is just a local file – no server or client architecture needed. From the SQLite website:

SQLite is a in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private. SQLite is currently found in more applications than we can count, including several high-profile projects

SQLite is easy to set up with Tcl and C/C++ interfaces and boasts a powerhouse of a user base including Adobe, Firefox, Microsoft, Apple, Google, and McAfee, just to name a few.

Just to show how easy SQLite is to use in a C++ program, here are a few lines of code that will help you understand how easy SQLite is to use and how it might be a good fit for your needs:

#include <sqlite3.h>
sqlite3* db;
char* db_err;
int main() {
  sqlite3_open(“my_db.sql3”, &db);
  sqlite3_exec(db, "create table 'helloworld' (id integer);", NULL, 0, &db_err)
  sqlite3_close(db);
}

And that’s a wrap. You pass any valid SQL query to the sqlite3_exec() function to do all your database manipulation. Data retrieval is passed via the parameters in a user-specified a callback function (in this example it was not used and was thus NULL). At any rate this simple example should be enough to help you get started with SQLite for your new project.

  • Twitter
  • Digg
  • del.icio.us
  • Propeller
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Mixx
  • Yahoo! Buzz
  • NewsVine
  • Facebook
  • Google Bookmarks

Related Posts


Related Stories

2 Responses to “SQLite with C++ Example: A Compact Serverless Database Alternative”

  • Comment from Kotori

    I found your howto to be very straightforward but lacking information that some people may find even more helpful than simply creating a table.
    How about illustrating SELECTs and/or UPDATES? Just an idea.

    • Comment from Jfinger

      Would appreciate a SELECT example using a callback function to get me started!


Leave a Reply

or Login (not required)





HTML tags allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>