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

📄 makesoftpagedlg.cpp

📁 sqlite读写VC例子,很不错
💻 CPP
字号:
// MakeSoftPageDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MakeSoftPage.h"
#include "MakeSoftPageDlg.h"

#include <string>
#include <vector>
#include <sstream>

#include	<fcntl.h>
#include	<sys/stat.h>
#include	<io.h>

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

/////////////////////////////////////////////////////////////////////////////
// CMakeSoftPageDlg dialog

CMakeSoftPageDlg::CMakeSoftPageDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMakeSoftPageDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMakeSoftPageDlg)
	m_Sname = _T("");
	m_Sauthor = _T("");
	m_Sinfo = _T("");
	m_Ssize = _T("MB");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMakeSoftPageDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMakeSoftPageDlg)
	DDX_Control(pDX, IDC_LIST_DATA, m_List_Date);
	DDX_Text(pDX, IDC_NAME, m_Sname);
	DDX_Text(pDX, IDC_AUTHOR, m_Sauthor);
	DDX_Text(pDX, IDC_INFO, m_Sinfo);
	DDX_Text(pDX, IDC_MBKB, m_Ssize);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMakeSoftPageDlg, CDialog)
	//{{AFX_MSG_MAP(CMakeSoftPageDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_READ, OnRead)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMakeSoftPageDlg message handlers

BOOL CMakeSoftPageDlg::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
	
	// TODO: Add extra initialization here
	//增加列表栏的扩展属性,使之可以上下左右滚动
	DWORD dwStyle = m_List_Date.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	m_List_Date.SetExtendedStyle(dwStyle);

	//设置列表栏的项目标题
	m_List_Date.InsertColumn(0, _T("ID"), LVCFMT_CENTER, 25);
	m_List_Date.InsertColumn(1, _T("软件名称"), LVCFMT_CENTER, 90);
	m_List_Date.InsertColumn(2, _T("大小"), LVCFMT_CENTER, 45);
	m_List_Date.InsertColumn(3, _T("作者"), LVCFMT_CENTER, 60);
	m_List_Date.InsertColumn(4, _T("软件简介"), LVCFMT_CENTER, 130);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMakeSoftPageDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMakeSoftPageDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMakeSoftPageDlg::OnOK() 
{
	UpdateData(TRUE);

	if (!CheckVaild())
	{
		MessageBox("输入信息不完整!");
		
		return;
	}

	// TODO: Add extra validation here
	initDataBase();

	int rc;
	sqlite3_stmt *stat;
	char exec[1024]="", insert_id[16]="";

	sprintf(exec, "INSERT INTO [software] ([sname], [ssize], [sauthor], [sinfo]) VALUES(?,?,?,?);");

	rc = sqlite3_prepare(db, exec, -1, &stat, 0);

	rc = sqlite3_bind_text(stat, 1, m_Sname.GetBuffer(m_Sname.GetLength()), -1, NULL);
	rc = sqlite3_bind_text(stat, 2, m_Ssize.GetBuffer(m_Ssize.GetLength()), -1, NULL);
	rc = sqlite3_bind_text(stat, 3, m_Sauthor.GetBuffer(m_Sauthor.GetLength()), -1, NULL);
	rc = sqlite3_bind_text(stat, 4, m_Sinfo.GetBuffer(m_Sinfo.GetLength()), -1, NULL);

	rc = sqlite3_step(stat);

	rc = sqlite3_finalize(stat);

	sprintf(insert_id, "%ld", sqlite3_last_insert_rowid(db));

	closeDatabase();
	
	MessageBox("已将记录存入数据库!");

	m_Sname = _T("");
	m_Sauthor = _T("");
	m_Sinfo = _T("");
	m_Ssize = _T("MB");
	
	UpdateData(FALSE);
	//CDialog::OnOK();
}

void CMakeSoftPageDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

bool CMakeSoftPageDlg::CheckVaild()
{
	if (m_Sname == "")
	{
		return FALSE;
	}
	else if (m_Sauthor == "")
	{
		return FALSE;
	}
	else if (m_Sinfo == "")
	{
		return FALSE;
	}
	else if (m_Ssize == "MB")
	{
		return FALSE;
	}

	return TRUE;
}

BOOL CMakeSoftPageDlg::CheckFile(char * file)
{
	bool r = false;

	if (_access(file, 0) != -1)
	{
		r = true;
	}

	return r;
}

int CMakeSoftPageDlg::initDataBase()
{
	if (CheckFile(".\\test.db"))
	{
		sqlite3_open(".\\test.db", &db);

		return 0;
	}
	else
	{
		char *zErrMsg = 0;
		int rc;

		sqlite3_open(".\\test.db", &db);

		if(db == NULL)
		{ 
			return -1;
		}

		rc = sqlite3_exec(db, "BEGIN;", 0, 0, &zErrMsg);

		rc = sqlite3_exec(db, "CREATE TABLE software ([id] INTEGER PRIMARY KEY, "
			"[sname] varchar(256), [ssize] varchar(64), [sauthor] varchar(64), [sinfo] text);", 
			0, 0, &zErrMsg);

		rc = sqlite3_exec(db, "COMMIT;", 0, 0, &zErrMsg);

		return 0;
	}
}

int CMakeSoftPageDlg::closeDatabase()
{
	return sqlite3_close(db);
}

void CMakeSoftPageDlg::OnRead() 
{
	// TODO: Add your control notification handler code here
	m_List_Date.DeleteAllItems();

	initDataBase();

	LVITEM lvi;
	int nrow = 0, ncolumn = 0, i = 0;
	char ** azResult;
	char *zErrMsg = 0;

	sqlite3_get_table(db, "select * from software;", &azResult, &nrow, &ncolumn, &zErrMsg);

	for(i = 1; i <= nrow; i++)
	{
		lvi.mask = LVIF_TEXT;		//以结构体的方式插入列表项
		lvi.iItem = i-1;
		lvi.pszText=(LPTSTR)(LPCTSTR)(azResult[i*ncolumn]);
		lvi.iSubItem = 0;

		this->m_List_Date.InsertItem(&lvi);
		this->m_List_Date.SetItemText(i-1,1,azResult[i*ncolumn+1]);
		this->m_List_Date.SetItemText(i-1,2,azResult[i*ncolumn+2]);
		this->m_List_Date.SetItemText(i-1,3,azResult[i*ncolumn+3]);
		this->m_List_Date.SetItemText(i-1,4,azResult[i*ncolumn+4]);
	}

	sqlite3_free_table(azResult);
	
	closeDatabase();
}

void CMakeSoftPageDlg::OnDelete() 
{
	// TODO: Add your control notification handler code here
	//获取当前选择的项目
	int n=m_List_Date.GetSelectionMark();

	//如果确实有项目存在
	if(n!=-1)
	{
		CString s_id,s_name,s_size,s_author,s_info;
		
		//分别获取它们的值
		s_id=m_List_Date.GetItemText( n, 0 );
		s_name=m_List_Date.GetItemText( n, 1 );
		s_size=m_List_Date.GetItemText( n, 2 );
		s_author=m_List_Date.GetItemText( n, 3 );
		s_info=m_List_Date.GetItemText( n, 4 );

		char *zErrMsg = 0;
		int rc;
		sqlite3_stmt *stat;
		char exec[1024]="";

		initDataBase();

		rc = sqlite3_exec(db, "BEGIN;", 0, 0, &zErrMsg);
	
		sprintf(exec, "DELETE FROM software where id=?;");

		rc = sqlite3_prepare(db, exec, -1, &stat, 0);
		rc = sqlite3_bind_int(stat, 1, atoi(s_id.GetBuffer(s_id.GetLength())));
		rc = sqlite3_step(stat);

		rc = sqlite3_exec(db, "COMMIT;", 0, 0, &zErrMsg);

		closeDatabase();

		this->OnRead();
	}
}

void CMakeSoftPageDlg::OnUpdate() 
{
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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