Click here to Skip to main content
13,863,433 members
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 11 Apr 2016
Licenced CPOL


, 11 Apr 2016
Rate this:
Please Sign up or sign in to vote.
SQLite access C++ classes


These C++/MFC classes simplify access to SQLite databases from your MFC applications. SQLite native API is quite simple and documentation is very good so it will take you about one day to start using it - and with these two classes, you will do the same job in minutes. ;)

Please download the full source code with sample application here.

Using the Code

To add SQLite access to your MFC application, please add the files from the sample application \database\ folder to your project. After that, please:

  1. #include the SQLiteDatabase.h and SQLiteRecordset.h to your source code
  2. Create an instance of the CSQLiteDatabase class. It will be a good idea to make it a member of your CWinApp application class or a member of an application window class
  3. Connect to the database by using the CSQLiteDatabase::Connect() method.
  4. If you need to launch a query that does not return a recordset, please use the BOOL CSQLiteDatabase::Query(LPCTSTR szQuery, CString &sError) method
  5. If you need to launch a query that returns a recordset, please:
    • Create an uninitialized (NULL) pointer to an instance of the CSQLiteRecordset class
    • Use the BOOL CSQLiteDatabase::Query(LPCTSTR szQuery, CString &sError, CSQLiteRecordset **ppRecordset) method, passing the pointer to the pointer you've created above in the 3rd query parameter
    • Navigate through the records by using the CSQLiteRecordset::Next() method
    • Read the data from the database by using the CSQLiteRecordset::AsInteger(int number), CSQLiteRecordset::AsString(int number), etc. methods
    • Use the CSQLiteRecordset::Close() method after you'll finish with reading data.
  6. After your database job will be completed, please close the database connection by using the CSQLiteDatabase::Disconnect() method.

Usage Sample

Below, please find the code that demonstrates the class usage. This code fragment opens a database, prints records from this database on the screen and then closes connection to the database:

#include "SQLiteDatabase.h"
#include "SQLiteRecordset.h"

void printDatabase(void)
//	connecting to the database
	CString sError;
	CSQLiteDatabase sqlDatabase;
	if (!sqlDatabase.Connect("C:\\database.sqlite", NULL, NULL, sError)
		AfxMessageBox("Failed to connect to the database: " + sError);
//	opening the recordset
	CSQLiteRecordset *pRecordset = NULL;
	if (!sqlDatabase.Query
	("SELECT last_name FROM employee", sError, &pRecordset))
		AfxMessageBox("Failed to query the database: " + sError);
//	looping thru records
	while (pRecordset->Next())
		AfxMessageBox(pRecordset->AsString(0));  //  displaying the recordset 
							 // field #0 value on the screen
//	closing the recordset and disconnecting from the database
	delete pRecordset;
	pRecordset = NULL;


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


About the Author

Serge V. Sushko
Software Developer (Senior) Oxetta
Russian Federation Russian Federation
An independed software vendor developing business applications for various industries. My current project is Oxetta Report Generator, the free reporting engine for C/C++ developers:

You may also be interested in...

Comments and Discussions

QuestionWrite to database Pin
Roman Tarasov15-Jun-16 8:17
memberRoman Tarasov15-Jun-16 8:17 
AnswerRe: Write to database Pin
Serge V. Sushko15-Jun-16 10:45
memberSerge V. Sushko15-Jun-16 10:45 
QuestionCan't download the code Pin
Gaap13-Apr-16 3:14
memberGaap13-Apr-16 3:14 
AnswerRe: Can't download the code Pin
rather_b_sailing13-Apr-16 7:37
memberrather_b_sailing13-Apr-16 7:37 
PraiseI will take a look at this Pin
rather_b_sailing12-Apr-16 7:34
memberrather_b_sailing12-Apr-16 7:34 
GeneralRe: I will take a look at this Pin
Serge V. Sushko12-Apr-16 9:02
memberSerge V. Sushko12-Apr-16 9:02 

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 | Cookies | Terms of Use | Mobile
Web01 | 2.8.190214.1 | Last Updated 11 Apr 2016
Article Copyright 2016 by Serge V. Sushko
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid