Click here to Skip to main content
13,446,005 members (38,865 online)
Click here to Skip to main content
Add your own
alternative version


73 bookmarked
Posted 31 Jul 2004

Another Embedded Database For C++

, 13 Jan 2007
Rate this:
Please Sign up or sign in to vote.
A C++ wrapper to SQLite database


Here is another wrapper for the famous SQLite database engine. Yes, I know about CppSQLite. It is an excellent wrapper for SQLite. I do not claim my code is superior to CppSQLite in some way. But if you already know the JDBC API, then you will find my library really easy and straightforward to use.

Using the code

Let us jump right into some example snippets that will help you start using the classes right away. To create or open a database, use the Connection class' open() function.

Connection conn;"mydb"); // the argument is the database name.

To execute SQL commands, get a handle to the Statement object. Call the execute() function to execute all SQL commands except SELECT. The execute() function will return the number of rows affected by the query.

Statement* stmt = conn.createStatement();
stmt->execute("create table albums(title,artist)"); // creates a table
 "insert into albums values('Brothers in Arms','Dire Straits')");
    // insert some data
stmt->execute("insert into albums values('Unplugged','Eric Clapton')");

To execute a SELECT statement, use the executeQuery() function. This function returns a handle to a ResultSet. use the next() function to navigate through the ResultSet. The ResultSet class has a number of getXXX() functions that take the number of the column as it's argument and return that column's value as the specified type. The different getXXX() functions are:

  • std::string getString(int colNum)
  • int getInt(int colNum)
  • long getLong(int colNum)
  • unsigned int getUInt(int colNum)
  • unsigned long getULong(int colNum)
  • float getFloat(int colNum)
  • double getDouble(int colNum)
  • bool getBoolean(int colNum)

The ResultSet has an object of ResultSetMetaData embedded with in it. We can get a handle to this object with a call to getMetaData(). The ResultSetMetaData contains information like the number of columns in the ResultSet, their names and data types.

ResultSet* rslt = stmt->executeQuery("select * from albums");
ResultSetMetaData* r_mtdt = rslt->getMetaData();
int cols = r_mtdt->getColumnCount();
while(rslt->next()) {
  for(int i=0;i<cols;i++) {
    printf("%s (%s): %s  ",r_mtdt->getColumnName(i+1).c_str(),

The DatabaseMetaData class contains the meta data of the entire database. We can obtain a DatabaseMetaData handle by calling the getDatabaseMetaData() function of the Connection class.

Call the refreshMetaData() function first, if you want the latest database meta data.

    // check to see if a table exists
  stmt->execute("drop table albums");// delete it if it exists

The DatabaseMetaData contains the following information:

  • Number of objects in the database.
  • Name of each object
  • Type of each object (table,index)

We can use the doesObjectExist() function to check if an object exists in the database. The first argument of this function is the name of the object and the second argument is it's type. This can be either "table" or "index".

Points of Interest

  1. To use this library, include the header file dsqlxprez_2.h and link your project with sqlite3.lib
  2. Always enclose database calls in a try - catch block that catches a SQLException
  3. All the classes in the library falls in a namespace called dsqlxprez. (DSQL stands for DarkSide SQL. Just a name, that's' it !!)
  4. You do not have to call the close() function on the Connection object explicitly. All resources are automatically deleted by the library when the Connection object's destructor is executed.


  • Created July 26, 2004.
  • Updated downloads Jan 14, 2007


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

QuestionPrinting the affected rows Pin
Member 1190125122-Feb-18 17:29
memberMember 1190125122-Feb-18 17:29 
GeneralThank you ! Pin
Memocs24-Jun-07 16:46
memberMemocs24-Jun-07 16:46 
QuestionSQLITE3.DLL Pin
usf_azi9-Feb-07 8:03
memberusf_azi9-Feb-07 8:03 
GeneralI prefer Rob Groves version... Pin
Aaron Planell16-Jan-07 21:40
memberAaron Planell16-Jan-07 21:40 
GeneralRe: I prefer Rob Groves version... Pin
shiftedbitmonkey5-Jan-09 13:43
membershiftedbitmonkey5-Jan-09 13:43 
GeneralSlow insert fast select Pin
GVR197912-Jan-07 4:03
memberGVR197912-Jan-07 4:03 
GeneralRe: Slow insert fast select Pin
islobodan14-Jan-07 8:54
memberislobodan14-Jan-07 8:54 
Generallast_insert_rowid Pin
GVR197910-Jan-07 6:55
memberGVR197910-Jan-07 6:55 
GeneralRe: last_insert_rowid Pin
Vijay Mathew Pandyalakal11-Jan-07 0:41
memberVijay Mathew Pandyalakal11-Jan-07 0:41 
GeneralRe: last_insert_rowid Pin
GVR197912-Jan-07 4:54
memberGVR197912-Jan-07 4:54 
GeneralRe: last_insert_rowid Pin
Vijay Mathew Pandyalakal15-Jan-07 16:57
memberVijay Mathew Pandyalakal15-Jan-07 16:57 
Generalsqlite3.dll and sqlite3.lib for vc++ 6.0 Pin
Thai Duong Nguyen3-Jan-07 17:54
memberThai Duong Nguyen3-Jan-07 17:54 
GeneralRe: sqlite3.dll and sqlite3.lib for vc++ 6.0 Pin
Vijay Mathew Pandyalakal4-Jan-07 17:18
memberVijay Mathew Pandyalakal4-Jan-07 17:18 
GeneralRe: sqlite3.dll and sqlite3.lib for vc++ 6.0 Pin
Aaron Planell16-Jan-07 21:43
memberAaron Planell16-Jan-07 21:43, Please Pin
Uwe Keim2-Jan-07 21:58
sitebuilderUwe Keim2-Jan-07 21:58 
GeneralRe:, Please Pin
Vijay Mathew Pandyalakal3-Jan-07 17:19
memberVijay Mathew Pandyalakal3-Jan-07 17:19 
QuestionWhat has changed ... Pin
Hans Dietrich2-Jan-07 18:29
memberHans Dietrich2-Jan-07 18:29 
AnswerRe: What has changed ... Pin
Vijay Mathew Pandyalakal3-Jan-07 17:13
memberVijay Mathew Pandyalakal3-Jan-07 17:13 
GeneralMemory Leak Pin
jbay10130-Dec-06 18:52
memberjbay10130-Dec-06 18:52 
GeneralRe: Memory Leak Pin
Vijay Mathew Pandyalakal1-Jan-07 19:33
memberVijay Mathew Pandyalakal1-Jan-07 19:33 
Generalwindows mobile Pin
GVR197929-Nov-06 11:08
memberGVR197929-Nov-06 11:08 
GeneralRe: windows mobile Pin
Vijay Mathew Pandyalakal29-Nov-06 16:53
memberVijay Mathew Pandyalakal29-Nov-06 16:53 
GeneralRe: windows mobile Pin
GVR197930-Nov-06 3:43
memberGVR197930-Nov-06 3:43 
GeneralRe: windows mobile Pin
Vijay Mathew Pandyalakal30-Nov-06 17:11
memberVijay Mathew Pandyalakal30-Nov-06 17:11 
QuestionLicense? Pin
Sceptic Mole14-Nov-06 10:42
memberSceptic Mole14-Nov-06 10:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180314.2 | Last Updated 14 Jan 2007
Article Copyright 2004 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid