Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC Sqlite
I am looking for OLE DB driver to acces SQLite. Is there any? I need it to use under MFC, not .NET.
Or another way to just do a simple select in SQLite data base!
Posted 2-Nov-11 1:43am
vahidmn359
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

No need for OLE. Using SQLite couldn't be simpler.
Simply grab the sqlite-amalgamation code from the download page[^] (the top one), add the sqlite3.c file to your solution so that their code is compiled as part of your solution, and #include at least sqlite3.h, possibly sqlite3ext.h too depending on what you are doing (or just include it anyway to be safe).
Don't add the shell.c file to your project. This contains the code for a standalone shell (command prompt) for interacting with SQLite databases.
 
The code configures itself automatically for your compiler, and should just work.
 
Take a look at the quickstart[^] to see how to use it.
 
It supports a limited set of SQL commands, however is all I have ever needed from it.
  Permalink  
Comments
vahidmn at 2-Nov-11 8:35am
   
Thanks, but it causes some error. and I dont know Why these error happend! I copy one of the errors which givin for this line:
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("d:\\1\\test.db", &db);//error here was happened
 
Error 8 error LNK2019: unresolved external symbol _sqlite3_open referenced in function "public: void __thiscall CTTSPDlg::OnBnClickedButton2(void)" (?OnBnClickedButton2@CTTSPDlg@@QAEXXZ) TTS-PDlg.obj
vahidmn at 2-Nov-11 9:28am
   
and for adding sqlite3.c gives this error:
Error 1 fatal error C1853: 'Debug\TTS-P.pch' precompiled header file is from a previous version of the compiler, or the precompiled header is C++ and you are using it from C (or vice versa) d:\tts\programming\delete1\sqlit\sqlite3.c 1
 
Why?
Andrew Brock at 2-Nov-11 21:52pm
   
Right click on the file and go to properties.
Change the configuration to all configurations at the top left
Over the left, expand Configuration Properties>C/C++>Precompiled Headers.
Set the Precompiled Header property to "Not using precompuled headers"
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I've got it!
Download from http://www.sqlite.org/download.html first file : sqlite-amalgamation-3071100.zip(1.30 MiB) .This ZIP archive contains all C source code for SQLite 3.7.11 combined into a single source file. Unzip it to the your project directory (where your .cpp and .h files are). Then, in Visual Studio 2010 create project MFC application and add into your .cpp file :
#include sqlite3.h
. In Solution Explorer in Header Files add sqlite3.h file . And in Source Files add sqlite3.c file. In Source Files by right clicking choose Property of sqlite3.c file and there in C/C++ section in /Precompiled Headers at right side in Precompiled Header choose Not Using Precompiled Headers.
In menu of Visual Studio click Project and at the bottom click on Properties, and in opened window in Configuration choose All Configurations. In General --> Use of MFC should be as Shared DLL , then Use of ATL as Not Using ATL, Common Language Runtime as No CLR.
At C/C++ --> Code Generation --> Runtime Library should be set Multi-Threaded DLL (/MD).
Add this code at the end of your .cpp file and debug your project:
// This is the callback function to display the select data in the table 
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
	int i;
	for(i=0; i<argc;>	{
		printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
	}
	printf("\n");
	return 0;
}
 

int _tmain(int argc, char* argv[])
{
	sqlite3 *db; // sqlite3 db struct
	char *zErrMsg = 0;
	int rc;
 
	// Open the test.db file
	rc = sqlite3_open("test.db", &db);
 
	if( rc ){
		// failed
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
	} 
	else
	{
		// success
		fprintf(stderr, "Open database successfully\n\n");
	}
 
	// ..
	// .. after open database
	// ..

	const char *pSQL[6];
 
	// Create a new myTable in database
	pSQL[0] = "create table myTable (FirstName varchar(30), LastName varchar(30), Age smallint)";
 
	// Insert first data item into myTable
	pSQL[1] = "insert into myTable (FirstName, LastName, Age) values ('Woody', 'Alan', 45)";
	
	// Insert second data item into myTable
	pSQL[2] = "insert into myTable (FirstName, LastName, Age) values ('Micheal', 'Bay', 38)";
 
	// Select all data in myTable
	pSQL[3] = "select * from myTable";
	
	// Remove all data in myTable
	pSQL[4] = "delete from myTable"; 
 
	// Drop the table from database
	pSQL[5] = "drop table myTable";
 
	// execute all the sql statement
	for(int i = 0; i < 4; i++)
	{
		rc = sqlite3_exec(db, pSQL[i], callback, 0, &zErrMsg);
		if( rc!=SQLITE_OK ){
			fprintf(stderr, "SQL error: %s\n", zErrMsg);
			sqlite3_free(zErrMsg);
			break;
		}
	}
 
	// ..
	// .. before close database
	// ..

	// Open the test.db file
	sqlite3_close(db);
 
	getchar();
 
	return 0;
}
And add test.bd file to your project.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 26 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100