微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MFC SQlite3封装工程

最近要做一个小工具,用到数据库sql数据库太麻烦,只是一个简单的数据库太多的功能也用不到,因此决定用sqlite3,以前在linux下玩过,但是不太深入,这次老大把活派下来了,没办法硬着头皮往前冲吧,于是开始在网上找相关的资料,找到了两个历程但是不是很理想,封装的水平感觉有限,最后在外国的网站上发现了一点干货

Kompex sqlite Wrapper for C++

http://sqlitewrapper.kompex-online.com/index.PHP?content=home


非常完美

数据库的部分代码如下:

void CsqliteMFCDlg::OnBnClickedButtonRead()
{
	// Todo: 在此添加控件通知处理程序代码

	Kompex::sqliteDatabase *pDatabase = new Kompex::sqliteDatabase("world.db3",sqlITE_OPEN_READWRITE | sqlITE_OPEN_CREATE,0);
	
	// move database to memory,so that we are work on the memory database hence
	pDatabase->MoveDatabasetoMemory();
	
	// create statement instance for sql queries/statements
	Kompex::sqliteStatement *pStmt = new Kompex::sqliteStatement(pDatabase);

	
	CString res,name;
	int id;
#if 0
	//res = pStmt->GetsqlResultString("SELECT NAME FROM sheet1 WHERE ID = 1;");

	pStmt->sql("SELECT * FROM sheet1;");

	// process all results
	while(pStmt->FetchRow())
	{
		id = pStmt->GetColumnInt("ID");
		name = pStmt->GetColumnCString("NAME");
	}

	// do not forget to clean-up
	pStmt->FreeQuery();
	
#endif	
	//pStmt->sql(_T("SELECT * FROM sheet1 WHERE NAME LIKE '黄进';"));
	pStmt->sql(_T("SELECT * FROM Country;"));


	m_List.DeleteallItems();
	while(m_List.DeleteColumn(0));

	CRect rect;
	m_List.GetwindowRect(&rect);
	int colCnt = pStmt->GetColumnCount();
	int width = rect.Width() / colCnt;
	int col = 0;
	int row = 0;
	CString szText;


	if(width < 160) width = 160;
	for(col = 0; col < colCnt; coL++)
	{
		szText = pStmt->GetColumnName16(col);
		m_List.InsertColumn(col,szText,LVCFMT_LEFT,width);
	}
	while(pStmt->FetchRow())
	{
		szText = pStmt->GetColumnString16(0);
		m_List.InsertItem(row,szText);
		for(col = 1; col < colCnt; coL++)
		{
			szText = pStmt->GetColumnString16(col);
			m_List.SetItem(row,col,LVIF_TEXT,0);
		}
		++row;
	}

	pStmt->FreeQuery();

}


完整工程的下载地址:http://download.csdn.net/detail/fz835304205/8839049

每天进步一点点,进步来源于痛苦~P

原文地址:https://www.jb51.cc/sqlite/199529.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐