Click here to Skip to main content
Click here to Skip to main content

SQLiteTbl, A C++ class that provides a CRecordSet like interface for SQLite

By , 2 Dec 2008
 

SQLiteTbl-src

Introduction

I work on an application that has a CRecordSet interface to a Jet database engine through ODBC. It works correctly, and we have very few problems. But occasionally, we run into a customer that somehow corrupts their MDAC/ODBC installation, and it is a pain to get it corrected remotely.

With this in mind, I decided it would be nice to use a different database engine that was more under our own control. After doing some research, the choice of SQLite was obvious. The engine is completely free, and open source. It is also very fast, and can be compiled directly into your application. No more reliance on other system software. For more information, see this link.

SQLite is a free database. Here is their blurb from their website: "SQLite is an in-process library that implements a self-contained, server-less, 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."

This all sounded euphoric, until I realized that it wasn’t going to be very easy to re-write our code to stop using the CRecordSet interface. I looked around, but couldn’t find anything similar. With this in mind, I decided to create my own wrapper class.

I wanted SQLiteTbl to be very similar, but not necessarily an exact replacement for CRecordSet. I created SQLiteTbl from scratch. I did so without dependence on any other templates/libraries such as MFC or STL. I needed a container for the BLOB data as well as the string data, so I created my own SQLiteBlob and SQLiteString objects. SQLiteTbl provides the ability to have data member access to the data values in the specified table. You can sort, filter, delete, and add new records. You can also execute SQL statements directly, if needed.

Using the code

In order to use SQLiteTbl, you will need to download SQLite and add it to your project. SQLiteTbl expects to find sqlite3.h and sqliteInt.h in your include path. I have a sample MFC Visual Studio project that shows how to use SQLiteTble.

SQLiteTbl is an abstract base class. You will need to instantiate a derived SQLiteTbl in order for this work. You will need to have a derived SQLiteTbl for each table in your database.

Example class

Here is an example for a Cars table in the database:

cars.h:

class CarsTbl : public SQLiteTbl
{
public:
    CarsTbl();

    virtual bool Open(const char* pFile);
    virtual const char* TableName();
    virtual int ItemCount();
 
    //data members of the table
//0
    SQLiteString m_make;
    SQLiteString m_model;
    int m_year;
    double m_weight;
    SQLiteString m_color;
//5
    SQLiteString m_license;
    SQLiteBlob m_picture;
};

cars.cpp:

#include “SQLiteTbl.h”

CarsTbl::CarsTbl()
{
    //these must match table in .h
//0
    SetItem(0, "Make",              MYDB_TYPE_TEXT, &m_make);
    SetItem(1, "Model",             MYDB_TYPE_TEXT, &m_model);
    SetItem(2, "Year",              MYDB_TYPE_INT,  &m_year);
    SetItem(3, "Weight",            MYDB_TYPE_DOUBLE, &m_weight);
    SetItem(4, "Color",             MYDB_TYPE_TEXT, &m_color);
//5
    SetItem(5, "License",           MYDB_TYPE_TEXT, &m_license);
    SetItem(6, "Color Picture",     MYDB_TYPE_BLOB, &m_picture);
}

int CarsTbl::ItemCount() { return 7; } //must match items listed above

const char* CarsTbl::TableName() { return "DMV Cars"; }

bool CarsTbl::Open(const char* pFile)
{
    if (SQLiteTbl::Open(pFile)) {
        if (Query()) 
            return true;
    }
    //anything goes wrong, close and return false
    Close();
    return false;
}

Here is some basic code for using it:

CarsTbl myTable;
If (myTable.Open(“c:\temp\cars.sqlite”)) {
    //access data
    myTable.MoveFirst();
    const char* pMake = myTable.m_make;
    const char* pModel = myTable.m_model;
    int year = myTable.m_year;
    ...
    //add new item
    myTable.AddNew();
    myTable.m_make = “Ford”;
    myTable.m_model = “Focus”;
    myTable.m_year = 2002;
    myTable.Update();
    //get the table count
    int count = myTable.GetCount();
    //sort
    myTable.SetSort(“[Make] ASC, [Year] DESC”);
    myTable.Query();
    //filter
    myTable.SetFilter(“[Make]=\’Ford\’, [Model]=\’Mustang\’”);
    myTable.Query();
}

Sample project, "SQLiteTbl Test"

I have included a sample project that will compile on Visual Studio 2005, or Visual C++ 6.0. You should get the latest version of SQLite from here. There is a readme.txt in the project that describes how to build the project. It is a simple dialog based application that uses the SQLiteTbl class as CarsTbl.

Points of interest

On a side note, there is a Firefox plug-in called "SQLite Manager", which I found very useful when working with SQLite databases.

If you haven't seen the SQLite license yet, here it is:

** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
****************************************************

Isn't that a great license?

History

  • Version 1.3 - December 2, 2008.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Brad Kremer
Software Developer (Senior) Fluke Networks
United States United States
Member
I live in beautiful Wenatchee, Washington. When I'm not writing software, you can find me mountain biking or snow skiing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionSQLiteTbl for VC2010 implementationmemberPatrik Källback3 Apr '13 - 0:08 
First I would like to thank Brad for great code.
 
I have modified the SQLiteTbl class so it works with precompiled header and with warning level 4.
I have also added a new datatype to MYDB_TYPE (MYDB_TYPE_BIGINT) so it is possible to read and write 64 bit integer values.
 
Email me if you need the code.
 
Cheers /Patrik
QuestionSQLite?membernovice519 Mar '11 - 18:25 
I just wanna ask if you know any sample VC++ code or demo for
reports using sqlite database..i've searched over the net but
to no avail.
 
please help..
 
thanks in advance.
GeneralGoodmemberloyal ginger19 Nov '09 - 10:21 
Helped me a lot in my projects. Thanks!
QuestionCould you add import to your class?memberavs431 Jul '09 - 6:25 
Hi, great work...
 
Have you thought about adding import with a definable separator to your class.
SQlite has these functions but as I am quite new to c++ I cannot implement it.
 
Perhaps you could give me a few pointers.
 
Thanks again..
 
Chris
GeneralThank youmemberMister Transistor17 Dec '08 - 11:36 
For providing a working VS6 workspace download. Other sqlite articles in CodeProject don't do this, or give broken projects. The sqlite3 site says nothing about VS of course.
 
I have not tried to use your class for my own purposes yet but you deserve recognition for delivering code that works.
 
Best wishes
Andrew
 
PS I still use VS6 because I'm waiting for the framework after .NET before I upgrade ... Smile | :)
Generalhellomemberbatsword14 Dec '08 - 14:30 
bodis!
QuestionSecurity?memberli000013 Dec '08 - 5:29 
Hi
 
Do you have any ways to set password to protect your database file?
 
Thanks,
AnswerRe: Security?memberBrad Kremer15 Dec '08 - 13:30 
See the following:
http://www.sqlite.org/cvstrac/wiki?p=SqliteWebSecurity[^]
GeneralSQLitememberJust someone else12 Dec '08 - 5:10 
I have never used SQLite myself but have read things about it.
 
How do you think SQLite compares to Access? What are the good vs the bads.
 
Currently I am using CRecordSet and Access datbases. The data I am storing is low frequency so access is fine for now and easy to develop against.
 
I also make use of the Access databases through Excel via ODBC. Does SQLite offer the same functionality?
 
CRecordSet also handles exceptions but it doesn't look like you have implemented discriptive exceptions in your object from the code in your article.
 
Thanks and nice article.
AnswerRe: SQLitememberJohn Crenshaw12 Dec '08 - 21:56 
I personally wouldn't be caught dead using Access or Excel as a database. If the job is too much for SQLite, it is too big for Access or Excel. If the job is small enough for SQLite, why use something more complicated? (Actually, I CAN see a purpose for using Excel (user readability of the raw file), but I still would resist it.)
 
As for the "good vs the bad," you should take a look at http://www.sqlite.org/whentouse.html[^]. I think this should answer all your questions.

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 2 Dec 2008
Article Copyright 2008 by Brad Kremer
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid