⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo2dlg.cpp

📁 SQLite数据库
💻 CPP
字号:
// Demo2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Demo2.h"
#include "Demo2Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "CppSQLite3U.h"

/////////////////////////////////////////////////////////////////////////////
// CDemo2Dlg dialog

CDemo2Dlg::CDemo2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDemo2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDemo2Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDemo2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemo2Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDemo2Dlg, CDialog)
	//{{AFX_MSG_MAP(CDemo2Dlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemo2Dlg message handlers

BOOL CDemo2Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#define FILE_DB_NAME	TEXT("unitech.db")

//获取程序当前路径
void GetCurrentDirectory(CString &szPath)
{
	wchar_t pBuf[256];
	GetModuleFileName(NULL,pBuf,sizeof(pBuf)/sizeof(wchar_t));
	szPath=pBuf;
	szPath = szPath.Left(szPath.ReverseFind('\\')+1);
}

void CDemo2Dlg::OnButton1() 
{
	CString strDbPath;
	GetCurrentDirectory(strDbPath);
	strDbPath += FILE_DB_NAME;

	CppSQLite3DB db;
	try
	{
		//打开或新建一个数据库
		db.open(strDbPath);
		
		//判断表名是否存在
		if(db.tableExists(L"tblTest"))
		{
			AfxMessageBox(L"Table: tblTest is existed!");
		}
		else //不存在
		{
			AfxMessageBox(L"Table: tblTest not existed!");
			//新建表
			db.execDML(L"create table tblTest(empno varchar(20), empname varchar(20))");
		}

		//插入一笔记录
		db.execDML(L"insert into tblTest values('编号', '姓名')");
		//插入一笔记录
		db.execDML(L"insert into tblTest values('精瑞电脑', 'Answer')");

		//删除一笔记录
		db.execDML(L"delete from tblTest where empno='编号'");

		//插入10笔记录(使用事务)
		TCHAR buf[256];
		db.execDML(L"begin transaction;");
		for (int i = 0; i < 10; i++)
        {
			memset(buf, 0, sizeof(buf));
			wsprintf(buf, L"insert into tblTest values ('no%d', 'name%d');", i, i);
			db.execDML(buf);
        }
		db.execDML(L"commit transaction;");

		//更新一笔记录
		db.execDML(L"update tblTest set empname='answer' where empno='no1'");
	
		//获取总笔数
		int count = db.execScalar(L"select count(*) from tblTest;");
		TCHAR szCount[50];
		memset(szCount, 0, sizeof(szCount));
		wsprintf(szCount, L"Count:%d", count);
		AfxMessageBox(szCount);

		//获取每一笔
		CppSQLite3Query q = db.execQuery(L"select * from tblTest");
		while (!q.eof())
        {
			AfxMessageBox(q.fieldValue(0));
			q.nextRow();
		}
		q.finalize();

		db.close();
		AfxMessageBox(L"OK");
	}
	catch(CppSQLite3Exception ex)
	{
		AfxMessageBox(ex.errorMessage());
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -