Click here to Skip to main content
15,884,298 members
Articles / Database Development / SQL Server
Article

Another Embedded Database For C++

Rate me:
Please Sign up or sign in to vote.
4.71/5 (18 votes)
13 Jan 2007BSD2 min read 163.3K   4.5K   72   46
A C++ wrapper to SQLite database

Introduction

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;
conn.open("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
stmt->execute(
 "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(),
    r_mtdt->getColumnType(i+1).c_str(),rslt->getString(i+1).c_str());
  }
  printf("\n");
}

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.

conn.refreshMetaData();
if(conn.getDatabaseMetaData()->doesObjectExist("albums","table")) 
    // 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.

History

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

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: License? Pin
AnOldGreenHorn14-Nov-06 17:45
AnOldGreenHorn14-Nov-06 17:45 
Generalerror in sqlite - ISO C++ forbids declaration of `open' with no type Pin
sultan_sk13-Nov-06 3:07
sultan_sk13-Nov-06 3:07 
GeneralRe: error in sqlite - ISO C++ forbids declaration of `open' with no type Pin
AnOldGreenHorn13-Nov-06 18:36
AnOldGreenHorn13-Nov-06 18:36 
GeneralUnicode Pin
gillnrb19-May-06 4:17
gillnrb19-May-06 4:17 
GeneralRe: Unicode Pin
Aaron Planell16-Jan-07 21:43
Aaron Planell16-Jan-07 21:43 
GeneralVery good Job! Pin
googlecode116-Mar-06 2:52
googlecode116-Mar-06 2:52 
GeneralRe: Very good Job! Pin
AnOldGreenHorn16-Mar-06 15:49
AnOldGreenHorn16-Mar-06 15:49 
QuestionError compiling in devcpp? Pin
the12be24-Jul-05 11:19
the12be24-Jul-05 11:19 
AnswerRe: Error compiling in devcpp? Pin
AnOldGreenHorn24-Jul-05 18:49
AnOldGreenHorn24-Jul-05 18:49 
GeneralCompiling using Devcpp Pin
Member 196603016-May-05 15:05
Member 196603016-May-05 15:05 
GeneralRe: Compiling using Devcpp Pin
AnOldGreenHorn16-May-05 17:46
AnOldGreenHorn16-May-05 17:46 
GeneralExcellent Work Pin
Cleve Blakemore24-Feb-05 15:02
sussCleve Blakemore24-Feb-05 15:02 
Generalusing in C# Pin
Adnan Siddiqi15-Jan-05 6:01
Adnan Siddiqi15-Jan-05 6:01 
GeneralRe: using in C# Pin
AnOldGreenHorn26-Jan-05 18:32
AnOldGreenHorn26-Jan-05 18:32 
AnswerRe: using in C# Pin
Steffen Lange4-Jan-07 23:40
Steffen Lange4-Jan-07 23:40 
Questionhow to insert a variable Pin
aqndyggg15-Nov-04 12:01
aqndyggg15-Nov-04 12:01 
AnswerRe: how to insert a variable Pin
AnOldGreenHorn15-Nov-04 16:51
AnOldGreenHorn15-Nov-04 16:51 
GeneralRe: how to insert a variable Pin
aqndyggg16-Nov-04 2:00
aqndyggg16-Nov-04 2:00 
GeneralWhich SQLite paltform 2.8.15 or 3.0.8 Pin
Sharad Kelkar6-Nov-04 18:34
Sharad Kelkar6-Nov-04 18:34 
GeneralRe: Which SQLite paltform 2.8.15 or 3.0.8 Pin
AnOldGreenHorn7-Nov-04 16:47
AnOldGreenHorn7-Nov-04 16:47 
GeneralRe: Which SQLite paltform 2.8.15 or 3.0.8 Pin
Sharad Kelkar7-Nov-04 18:39
Sharad Kelkar7-Nov-04 18:39 

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.