Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC

SQLite

Rate me:
Please Sign up or sign in to vote.
4.36/5 (14 votes)
11 Apr 2016CPOL1 min read 23.3K   1.8K   19   7
SQLite access C++ classes

Introduction

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:

C++
#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);
		return;
	}
  
//	opening the recordset
	CSQLiteRecordset *pRecordset = NULL;
	if (!sqlDatabase.Query
	("SELECT last_name FROM employee", sError, &pRecordset))
	{
		AfxMessageBox("Failed to query the database: " + sError);
		sqlDatabase.Disconnect();
		return;
	}
	
//	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
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	sqlDatabase.Disconnect();
}

License

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


Written By
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: http://www.oxetta.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Willieho17-Aug-21 15:36
Willieho17-Aug-21 15:36 
QuestionWrite to database Pin
Roman Tarasov15-Jun-16 7:17
Roman Tarasov15-Jun-16 7:17 
AnswerRe: Write to database Pin
Serge V. Sushko15-Jun-16 9:45
Serge V. Sushko15-Jun-16 9:45 
QuestionCan't download the code Pin
Gaap13-Apr-16 2:14
Gaap13-Apr-16 2:14 
AnswerRe: Can't download the code Pin
Midnight48913-Apr-16 6:37
Midnight48913-Apr-16 6:37 
PraiseI will take a look at this Pin
Midnight48912-Apr-16 6:34
Midnight48912-Apr-16 6:34 
GeneralRe: I will take a look at this Pin
Serge V. Sushko12-Apr-16 8:02
Serge V. Sushko12-Apr-16 8: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.