SQLiteTest源代码

先上效果图:

void CSQLiteTestDlg::OnBnClickedCreateButton()
{
	bool fTest;
	CDbSQLite sqlite;
	//连接打开SQLite数据库
	fTest = sqlite.Open(_T("theTestSqlite.db"));
	if (!fTest)
	{
		AfxMessageBox(_T("打不开theTestSqlite.db"));
		return;
	}
	//执行创建表语句
	fTest = sqlite.DirectStatement(_T("CREATE TABLE usersInfo(name varchar(30), password varchar(20))"));
	if (!fTest)
		AfxMessageBox(_T("不能创建表usersInfo"));
	//新增数据
	CString szQuery;
	szQuery = _T("INSERT INTO usersInfo VALUES ('小王','123')");
	fTest = sqlite.DirectStatement(szQuery);
	if (!fTest)
		AfxMessageBox(_T("插入数据失败!"));
	szQuery = _T("INSERT INTO usersInfo VALUES ('大王','322')");
	fTest = sqlite.DirectStatement(szQuery);
	if (!fTest)
		AfxMessageBox(_T("插入数据失败!"));
	AfxMessageBox(_T("创建表成功,插入数据成功!"));
}

void CSQLiteTestDlg::OnBnClickedLookButton()
{
	BOOL fTest;
	CDbSQLite sqlite;
	fTest = sqlite.Open(_T("theTestSqlite.db"));
	if (!fTest)
	{
		AfxMessageBox(_T("打不开theTestSqlite.db"));
		return;
	}
	CSqlStatement *stmt = sqlite.Statement(_T("SELECT * FROM usersInfo"));
	//当数据不为空的时候,填充LIST控件
	if (stmt != NULL)
	{
		m_ListCtrl.DeleteAllItems();  //清除LIST项
		while(m_ListCtrl.DeleteColumn(0));//清除LIST列项
		CRect rect;
		m_ListCtrl.GetWindowRect(&rect);  //获得LIST控件大小
		int nFields = stmt->Fields();  //取得数据库表的列数
		int nWidth = (rect.Width() - rect.Width() * 0.02) / nFields; //平均分布列数
		int nCol = 0;
		int nRow = 0;
		std::string szText;
		for(nCol = 0; nCol < nFields; nCol++)
		{
			szText = stmt->FieldName(nCol);		//得到列名
			m_ListCtrl.InsertColumn(nCol, CString(szText.c_str()), LVCFMT_LEFT, nWidth, nCol);
		}
		while (stmt->NextRow())
		{
			szText = stmt->ValueString(0);		//得到列值
			m_ListCtrl.InsertItem(nRow, CString(szText.c_str()));
			for(nCol = 1; nCol < nFields; nCol++)
			{
				szText = stmt->ValueString(nCol); //得到列值
				m_ListCtrl.SetItem(nRow, nCol, LVIF_TEXT, CString(szText.c_str()), 0, 0, 0, 0);
			}
			++nRow;
		}
	}
	UpdateData(FALSE);
}

 源代码来自于网络,仅仅用于记录分享。

posted @ 2020-01-09 14:49  秋月的私语  阅读(292)  评论(0编辑  收藏  举报