在MFC Visual C ++ 2008中连接到SQLite [英] connect to SQLite in MFC Visual C++ 2008

查看:70
本文介绍了在MFC Visual C ++ 2008中连接到SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找OLE DB驱动程序以访问SQLite.有没有?我需要它在MFC而不是.NET下使用.
或者只是在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!

推荐答案

不需要OLE.使用SQLite再简单不过了.
只需从下载页面 [ quickstart [ ^ ]了解如何使用它.

它支持一组有限的SQL命令,但是我只需要它.
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.


我已经掌握了它!
从http://www.sqlite.org/download.html下载第一个文件:sqlite-amalgamation-3071100.zip(1.30 MiB).此ZIP压缩文件包含SQLite 3.7.11的所有C源代码,并组合到一个源文件中.将其解压缩到您的项目目录(.cpp和.h文件所在的位置).然后,在Visual Studio 2010中创建项目MFC应用程序并将其添加到您的.cpp文件中:
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

.在解决方案资源管理器"的头文件"中,添加sqlite3.h文件.并在源文件中添加sqlite3.c文件.在源文件"中,通过右键单击选择sqlite3.c文件的属性,然后在预编译头"的/Precompiled头右侧的C/C ++部分中,选择不使用预编译头".
在Visual Studio菜单中,单击项目",然后在底部单击属性",然后在打开的窗口"的配置"中选择所有配置".一般来说-> MFC的使用应作为共享DLL,然后将ATL用作不使用ATL,将公共语言运行时用作不CLR.
在C/C ++中->代码生成->运行时库应设置为多线程DLL(/MD).
在您的.cpp文件末尾添加此代码并调试您的项目:

. 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;
}


并将test.bd文件添加到您的项目中.


And add test.bd file to your project.


这篇关于在MFC Visual C ++ 2008中连接到SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆