5,665,355 members and growing! (14,242 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate License: The BSD License

Another Embedded Database For C++

By Vijay Mathew Pandyalakal

A C++ wrapper to SQLite database
SQL, VC6, C++, Windows, SQL Server, Visual Studio, VS6, DBA, Dev

Posted: 31 Jul 2004
Updated: 14 Jan 2007
Views: 66,448
Bookmarked: 45 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 5.48 Rating: 4.45 out of 5
1 vote, 5.9%
1
0 votes, 0.0%
2
1 vote, 5.9%
3
1 vote, 5.9%
4
14 votes, 82.4%
5

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

About the Author

Vijay Mathew Pandyalakal



Occupation: Software Developer
Location: India India

Other popular Database articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
GeneralThank you !memberMemocs17:46 24 Jun '07  
QuestionSQLITE3.DLLmemberusf_azi9:03 9 Feb '07  
GeneralI prefer Rob Groves version...memberAaron Planell22:40 16 Jan '07  
GeneralSlow insert fast selectmemberGVR19795:03 12 Jan '07  
GeneralRe: Slow insert fast selectmemberislobodan9:54 14 Jan '07  
Generallast_insert_rowidmemberGVR19797:55 10 Jan '07  
GeneralRe: last_insert_rowidmemberVijay Mathew Pandyalakal1:41 11 Jan '07  
GeneralRe: last_insert_rowidmemberGVR19795:54 12 Jan '07  
GeneralRe: last_insert_rowidmemberVijay Mathew Pandyalakal17:57 15 Jan '07  
Generalsqlite3.dll and sqlite3.lib for vc++ 6.0memberThai Duong Nguyen18:54 3 Jan '07  
GeneralRe: sqlite3.dll and sqlite3.lib for vc++ 6.0memberVijay Mathew Pandyalakal18:18 4 Jan '07  
GeneralRe: sqlite3.dll and sqlite3.lib for vc++ 6.0memberAaron Planell22:43 16 Jan '07  
Generalwww.sqlite.org, PleasesitebuilderUwe Keim22:58 2 Jan '07  
GeneralRe: www.sqlite.org, PleasememberVijay Mathew Pandyalakal18:19 3 Jan '07  
QuestionWhat has changed ...memberHans Dietrich19:29 2 Jan '07  
AnswerRe: What has changed ...memberVijay Mathew Pandyalakal18:13 3 Jan '07  
GeneralMemory Leakmemberjbay10119:52 30 Dec '06  
GeneralRe: Memory LeakmemberVijay Mathew Pandyalakal20:33 1 Jan '07  
Generalwindows mobilememberGVR197912:08 29 Nov '06  
GeneralRe: windows mobilememberVijay Mathew Pandyalakal17:53 29 Nov '06  
GeneralRe: windows mobilememberGVR19794:43 30 Nov '06  
GeneralRe: windows mobilememberVijay Mathew Pandyalakal18:11 30 Nov '06  
GeneralLicense?memberSceptic Mole11:42 14 Nov '06  
GeneralRe: License?memberVijay Mathew Pandyalakal18:45 14 Nov '06  
Generalerror in sqlite - ISO C++ forbids declaration of `open' with no typemembersultan_sk4:07 13 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Jan 2007
Editor: Chris Maunder
Copyright 2004 by Vijay Mathew Pandyalakal
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project