文章目录
- 初始化
- 设置菜单
- 添加数据库类
- 创建数据库
- 配置数据库
- 全部代码
初始化
创建文件选择基于CListView
初始化数据
public:CListCtrl& m_list;
CSQLView::CSQLView() noexcept:m_list(GetListCtrl())
{// TODO: 在此处添加构造代码}
void CSQLView::OnInitialUpdate()
{CListView::OnInitialUpdate();m_list.ModifyStyle(0, LVS_REPORT);m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);m_list.InsertColumn(0, L"id", LVCFMT_LEFT, 100);m_list.InsertColumn(1, L"姓名", LVCFMT_LEFT, 100);m_list.InsertColumn(2, L"性别", LVCFMT_LEFT, 100);m_list.InsertColumn(3, L"年龄", LVCFMT_LEFT, 100);m_list.InsertColumn(4, L"部门", LVCFMT_LEFT, 100);m_list.InsertColumn(5, L"岗位", LVCFMT_LEFT, 100);m_list.InsertColumn(6, L"薪资", LVCFMT_LEFT, 100);
//修改数据库CMySQL mysql;if (FALSE == mysql.Connect("localhost", "root", "123456", "mangerdata", 3306, NULL, NULL)){MessageBox(L"连接数据库失败");return;}mysql.Query("set names GBK");//设置编码//插入数据CString sql=L"select * from user";//转换成窄字符串char str[500] = { 0 };WideCharToMultiByte(CP_ACP, 0, sql.GetBuffer(), sql.GetLength(), str, sizeof(str), NULL, NULL);int error = mysql.Query(str);if (error){MessageBox(L"查询失败");return;}mysql.StoreResult();int i = 0;//显示有问题while (mysql.FetchRow()){m_list.InsertItem(i,(wchar_t*)(mysql.m_mysql_row[0]));m_list.InsertItem(i, (wchar_t*)(mysql.m_mysql_row[1]));m_list.InsertItem(i, (wchar_t*)(mysql.m_mysql_row[2]));m_list.InsertItem(i, (wchar_t*)(mysql.m_mysql_row[3]));}
}
设置菜单
添加
创建添加类
每一个控件都添加控件变量
class CAddDlg : public CDialogEx
{DECLARE_DYNAMIC(CAddDlg)public:CAddDlg(CWnd* pParent = nullptr); // 标准构造函数virtual ~CAddDlg();// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_ADD_DLG };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持DECLARE_MESSAGE_MAP()
public:CString m_strName;CString m_strSex;int m_nAge;CString m_strDeparment;CString m_strJob;float m_fSalary;afx_msg void OnBnClickedAdd();};
// CAddDlg.cpp: 实现文件
//#include "pch.h"
#include "SQL.h"
#include "CAddDlg.h"
#include "afxdialogex.h"// CAddDlg 对话框IMPLEMENT_DYNAMIC(CAddDlg, CDialogEx)CAddDlg::CAddDlg(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_ADD_DLG, pParent), m_strName(_T("")), m_strSex(_T("")), m_nAge(0), m_strDeparment(_T("")), m_strJob(_T("")), m_fSalary(0)
{}CAddDlg::~CAddDlg()
{
}void CAddDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Text(pDX, IDC_EDIT2, m_strName);DDX_Text(pDX, IDC_EDIT1, m_strSex);DDX_Text(pDX, IDC_EDIT3, m_nAge);DDX_Text(pDX, IDC_EDIT4, m_strDeparment);DDX_Text(pDX, IDC_EDIT5, m_strJob);DDX_Text(pDX, IDC_EDIT6, m_fSalary);
}BEGIN_MESSAGE_MAP(CAddDlg, CDialogEx)ON_BN_CLICKED(IDC_ADD, &CAddDlg::OnBnClickedAdd)END_MESSAGE_MAP()// CAddDlg 消息处理程序void CAddDlg::OnBnClickedAdd()
{UpdateData();EndDialog(IDOK);
}
//添加
void CSQLView::OnAdd()
{CAddDlg dlg;if (IDCANCEL == dlg.DoModal())return;CMySQL mysql;if (FALSE == mysql.Connect("localhost", "root", "123456", "mangerdata", 3306, NULL, NULL)){MessageBox(L"连接数据库失败");return;}mysql.Query("set names GBK");//设置编码//插入数据CString sql;sql.Format(L"insert into user(name,sex,age,department,job,salary) value (\'%s\',\'%s\',%d,\'%s\',\'%s\',%f);",dlg.m_strName,dlg.m_strSex,dlg.m_nAge,dlg.m_strDeparment,dlg.m_strJob,dlg.m_fSalary);//转换成窄字符串char str[500] = { 0 };WideCharToMultiByte(CP_ACP, 0,sql.GetBuffer(),sql.GetLength(),str,sizeof(str),NULL,NULL);int error=mysql.Query(str);if(error){MessageBox(L"添加失败");return;}MessageBox(L"添加成功");//插入到列表int nCount = m_list.GetItemCount();CString str1;str1.Format(L"%d", nCount);m_list.InsertItem(nCount, str1);m_list.SetItemText(nCount, 1, dlg.m_strName);m_list.SetItemText(nCount, 2, dlg.m_strSex);str1.Format(L"%d", dlg.m_nAge);m_list.SetItemText(nCount, 3, str1);m_list.SetItemText(nCount, 4, dlg.m_strDeparment);m_list.SetItemText(nCount, 5, dlg.m_strJob);str1.Format(L"%.2f", dlg.m_fSalary);m_list.SetItemText(nCount, 6, str1);}
删除
//删除
void CSQLView::OnDelete()
{POSITION pos=m_list.GetFirstSelectedItemPosition();int nSel = m_list.GetNextSelectedItem(pos);if (nSel<0){MessageBox(L"先选中在删除");return;}CString strDelete = m_list.GetItemText(nSel, 1);CMySQL mysql;if (FALSE == mysql.Connect("localhost", "root", "123456", "mangerdata", 3306, NULL, NULL)){MessageBox(L"连接数据库失败");return;}mysql.Query("set names GBK");//设置编码//插入数据CString sql;sql.Format(L"delete from user where name=\'%s\';", strDelete);//转换成窄字符串char str[500] = { 0 };WideCharToMultiByte(CP_ACP, 0, sql.GetBuffer(), sql.GetLength(), str, sizeof(str), NULL, NULL);int error = mysql.Query(str);if (error){MessageBox(L"删除失败");return;}m_list.DeleteItem(nSel);
}
修改
设置添加对话框
添加修改类
// CModifyDlg 对话框
class CModifyDlg : public CDialogEx
{DECLARE_DYNAMIC(CModifyDlg)public:CModifyDlg(CWnd* pParent = nullptr); // 标准构造函数virtual ~CModifyDlg();// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_MODIFY_DLG };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持DECLARE_MESSAGE_MAP()
public:CString m_strName;CString m_strSex;int m_nAge;CString m_strDeparment;CString m_strJob;float m_fSalary;afx_msg void OnBnClickedModify();
};
#include "pch.h"
#include "SQL.h"
#include "CModifyDlg.h"
#include "afxdialogex.h"// CModifyDlg 对话框IMPLEMENT_DYNAMIC(CModifyDlg, CDialogEx)CModifyDlg::CModifyDlg(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_MODIFY_DLG, pParent), m_strName(_T("")), m_strSex(_T("")), m_nAge(0), m_strDeparment(_T("")), m_strJob(_T("")), m_fSalary(0)
{}CModifyDlg::~CModifyDlg()
{
}void CModifyDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Text(pDX, IDC_EDIT2, m_strName);DDX_Text(pDX, IDC_EDIT1, m_strSex);DDX_Text(pDX, IDC_EDIT3, m_nAge);DDX_Text(pDX, IDC_EDIT4, m_strDeparment);DDX_Text(pDX, IDC_EDIT5, m_strJob);DDX_Text(pDX, IDC_EDIT6, m_fSalary);
}BEGIN_MESSAGE_MAP(CModifyDlg, CDialogEx)ON_BN_CLICKED(IDC_MODIFY, &CModifyDlg::OnBnClickedModify)
END_MESSAGE_MAP()// CModifyDlg 消息处理程序void CModifyDlg::OnBnClickedModify()
{// TODO: 在此添加控件通知处理程序代码UpdateData();EndDialog(IDOK);
}
void CSQLView::OnModify()
{CModifyDlg dlg;//获取数据POSITION pos = m_list.GetFirstSelectedItemPosition();int nSel = m_list.GetNextSelectedItem(pos);if (nSel < 0){MessageBox(L"先选中在修改");return;}int workid = _wtoi(m_list.GetItemText(nSel, 0));dlg.m_strName = m_list.GetItemText(nSel,1);dlg.m_strSex= m_list.GetItemText(nSel, 2);dlg.m_nAge= _wtoi(m_list.GetItemText(nSel, 3));dlg.m_strDeparment= m_list.GetItemText(nSel, 4);dlg.m_strJob= m_list.GetItemText(nSel, 5);dlg.m_fSalary = _wtof(m_list.GetItemText(nSel, 6));if (IDCANCEL == dlg.DoModal())return;//修改数据库CMySQL mysql;if (FALSE == mysql.Connect("localhost", "root", "123456", "mangerdata", 3306, NULL, NULL)){MessageBox(L"连接数据库失败");return;}mysql.Query("set names GBK");//设置编码//插入数据CString sql;sql.Format(L"update user set sex=\'%s\',age=%d,department=\'%s\',job=\'%s\',salary=%f where name =\'%s\';",dlg.m_strSex, dlg.m_nAge, dlg.m_strDeparment, dlg.m_strJob, dlg.m_fSalary, dlg.m_strName);//转换成窄字符串char str[500] = { 0 };WideCharToMultiByte(CP_ACP, 0, sql.GetBuffer(), sql.GetLength(), str, sizeof(str), NULL, NULL);int error = mysql.Query(str);if (error){MessageBox(L"跟新失败");return;}
}
添加数据库类
创建数据库
//创建管理数据
create database mangerdata;//创建用户表
create table user(
workid int primary key auto_increment,
name varchar(10) not null default '',
sex char(1) not null default '',
age tinyint unsigned not null default 0,
department varchar(10) not null default '',
job varchar(10) not null default '',
salary float not null default 0.0
);
配置数据库
导入数据库头文件
设置静态链接
将dll放入X64Debug中
产看连接MySQL函数
#include “mysql.h”
class CMySQL
{
public:CMySQL();~CMySQL();
public://连接数据库BOOL Connect(const char *host,const char *user,const char *passwd,const char *db,unsigned int port,const char *unix_socket,unsigned long client_flag);//执行SQL语句int Query(const char* str);//保存结果集MYSQL_RES* StoreResult();//检索结果集MYSQL_ROW FetchRow();
private:MYSQL* m_mysql;MYSQL_RES* m_mysql_res;
public:MYSQL_ROW m_mysql_row;
};
#include "pch.h"
#include "CMySQL.h"#pragma comment(lib,"libmysql.lib")CMySQL::CMySQL()
{m_mysql = mysql_init(NULL);
}CMySQL::~CMySQL()
{
}BOOL CMySQL::Connect(const char * host, const char * user, const char * passwd, const char * db, unsigned int port, const char * unix_socket, unsigned long client_flag)
{if (NULL == mysql_real_connect(m_mysql, host, user, passwd, db, port, unix_socket, client_flag)){return FALSE;}return TRUE;}int CMySQL::Query(const char * str)
{return mysql_real_query( m_mysql,str,strlen(str));
}//保存结果集
MYSQL_RES * CMySQL::StoreResult()
{m_mysql_res=mysql_store_result(m_mysql);return m_mysql_res;
}MYSQL_ROW CMySQL::FetchRow()
{m_mysql_row=mysql_fetch_row(m_mysql_res);return m_mysql_row;
}
全部代码
// SQLView.cpp: CSQLView 类的实现
//#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "SQL.h"
#endif#include "SQLDoc.h"
#include "SQLView.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif#include "CAddDlg.h"
#include "CMySQL.h"
#include "CModifyDlg.h"// CSQLViewIMPLEMENT_DYNCREATE(CSQLView, CListView)BEGIN_MESSAGE_MAP(CSQLView, CListView)// 标准打印命令ON_COMMAND(ID_FILE_PRINT, &CListView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, &CListView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CListView::OnFilePrintPreview)ON_COMMAND(IDM_ADD, &CSQLView::OnAdd)ON_COMMAND(IDM_DELETE, &CSQLView::OnDelete)ON_COMMAND(IDM_MODIFY, &CSQLView::OnModify)
END_MESSAGE_MAP()// CSQLView 构造/析构CSQLView::CSQLView() noexcept:m_list(GetListCtrl())
{// TODO: 在此处添加构造代码}CSQLView::~CSQLView()
{
}BOOL CSQLView::PreCreateWindow(CREATESTRUCT& cs)
{// TODO: 在此处通过修改// CREATESTRUCT cs 来修改窗口类或样式return CListView::PreCreateWindow(cs);
}void CSQLView::OnDraw(CDC* /*pDC*/)
{CSQLDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: 在此处为本机数据添加绘制代码
}void CSQLView::OnInitialUpdate()
{CListView::OnInitialUpdate();m_list.ModifyStyle(0, LVS_REPORT);m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);m_list.InsertColumn(0, L"id", LVCFMT_LEFT, 100);m_list.InsertColumn(1, L"姓名", LVCFMT_LEFT, 100);m_list.InsertColumn(2, L"性别", LVCFMT_LEFT, 100);m_list.InsertColumn(3, L"年龄", LVCFMT_LEFT, 100);m_list.InsertColumn(4, L"部门", LVCFMT_LEFT, 100);m_list.InsertColumn(5, L"岗位", LVCFMT_LEFT, 100);m_list.InsertColumn(6, L"薪资", LVCFMT_LEFT, 100);//修改数据库CMySQL mysql;if (FALSE == mysql.Connect("localhost", "root", "123456", "mangerdata", 3306, NULL, NULL)){MessageBox(L"连接数据库失败");return;}mysql.Query("set names GBK");//设置编码//插入数据CString sql=L"select * from user";//转换成窄字符串char str[500] = { 0 };WideCharToMultiByte(CP_ACP, 0, sql.GetBuffer(), sql.GetLength(), str, sizeof(str), NULL, NULL);int error = mysql.Query(str);if (error){MessageBox(L"查询失败");return;}mysql.StoreResult();int i = 0;//显示有问题while (mysql.FetchRow()){m_list.InsertItem(i,(wchar_t*)(mysql.m_mysql_row[0]));m_list.InsertItem(i, (wchar_t*)(mysql.m_mysql_row[1]));m_list.InsertItem(i, (wchar_t*)(mysql.m_mysql_row[2]));m_list.InsertItem(i, (wchar_t*)(mysql.m_mysql_row[3]));}}//添加
void CSQLView::OnAdd()
{CAddDlg dlg;if (IDCANCEL == dlg.DoModal())return;CMySQL mysql;if (FALSE == mysql.Connect("localhost", "root", "123456", "mangerdata", 3306, NULL, NULL)){MessageBox(L"连接数据库失败");return;}mysql.Query("set names GBK");//设置编码//插入数据CString sql;sql.Format(L"insert into user(name,sex,age,department,job,salary) value (\'%s\',\'%s\',%d,\'%s\',\'%s\',%f);",dlg.m_strName,dlg.m_strSex,dlg.m_nAge,dlg.m_strDeparment,dlg.m_strJob,dlg.m_fSalary);//转换成窄字符串char str[500] = { 0 };WideCharToMultiByte(CP_ACP, 0,sql.GetBuffer(),sql.GetLength(),str,sizeof(str),NULL,NULL);int error=mysql.Query(str);if(error){MessageBox(L"添加失败");return;}MessageBox(L"添加成功");//插入到列表int nCount = m_list.GetItemCount();CString str1;str1.Format(L"%d", nCount);m_list.InsertItem(nCount, str1);m_list.SetItemText(nCount, 1, dlg.m_strName);m_list.SetItemText(nCount, 2, dlg.m_strSex);str1.Format(L"%d", dlg.m_nAge);m_list.SetItemText(nCount, 3, str1);m_list.SetItemText(nCount, 4, dlg.m_strDeparment);m_list.SetItemText(nCount, 5, dlg.m_strJob);str1.Format(L"%.2f", dlg.m_fSalary);m_list.SetItemText(nCount, 6, str1);}//删除
void CSQLView::OnDelete()
{POSITION pos=m_list.GetFirstSelectedItemPosition();int nSel = m_list.GetNextSelectedItem(pos);if (nSel<0){MessageBox(L"先选中在删除");return;}CString strDelete = m_list.GetItemText(nSel, 1);CMySQL mysql;if (FALSE == mysql.Connect("localhost", "root", "123456", "mangerdata", 3306, NULL, NULL)){MessageBox(L"连接数据库失败");return;}mysql.Query("set names GBK");//设置编码//插入数据CString sql;sql.Format(L"delete from user where name=\'%s\';", strDelete);//转换成窄字符串char str[500] = { 0 };WideCharToMultiByte(CP_ACP, 0, sql.GetBuffer(), sql.GetLength(), str, sizeof(str), NULL, NULL);int error = mysql.Query(str);if (error){MessageBox(L"删除失败");return;}m_list.DeleteItem(nSel);
}void CSQLView::OnModify()
{CModifyDlg dlg;//获取数据POSITION pos = m_list.GetFirstSelectedItemPosition();int nSel = m_list.GetNextSelectedItem(pos);if (nSel < 0){MessageBox(L"先选中在修改");return;}int workid = _wtoi(m_list.GetItemText(nSel, 0));dlg.m_strName = m_list.GetItemText(nSel,1);dlg.m_strSex= m_list.GetItemText(nSel, 2);dlg.m_nAge= _wtoi(m_list.GetItemText(nSel, 3));dlg.m_strDeparment= m_list.GetItemText(nSel, 4);dlg.m_strJob= m_list.GetItemText(nSel, 5);dlg.m_fSalary = _wtof(m_list.GetItemText(nSel, 6));if (IDCANCEL == dlg.DoModal())return;//修改数据库CMySQL mysql;if (FALSE == mysql.Connect("localhost", "root", "123456", "mangerdata", 3306, NULL, NULL)){MessageBox(L"连接数据库失败");return;}mysql.Query("set names GBK");//设置编码//插入数据CString sql;sql.Format(L"update user set sex=\'%s\',age=%d,department=\'%s\',job=\'%s\',salary=%f where name =\'%s\';",dlg.m_strSex, dlg.m_nAge, dlg.m_strDeparment, dlg.m_strJob, dlg.m_fSalary, dlg.m_strName);//转换成窄字符串char str[500] = { 0 };WideCharToMultiByte(CP_ACP, 0, sql.GetBuffer(), sql.GetLength(), str, sizeof(str), NULL, NULL);int error = mysql.Query(str);if (error){MessageBox(L"跟新失败");return;}}
// CAddDlg 对话框
class CAddDlg : public CDialogEx
{DECLARE_DYNAMIC(CAddDlg)public:CAddDlg(CWnd* pParent = nullptr); // 标准构造函数virtual ~CAddDlg();// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_ADD_DLG };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持DECLARE_MESSAGE_MAP()
public:CString m_strName;CString m_strSex;int m_nAge;CString m_strDeparment;CString m_strJob;float m_fSalary;afx_msg void OnBnClickedAdd();};
// CAddDlg.cpp: 实现文件
//#include "pch.h"
#include "SQL.h"
#include "CAddDlg.h"
#include "afxdialogex.h"// CAddDlg 对话框IMPLEMENT_DYNAMIC(CAddDlg, CDialogEx)CAddDlg::CAddDlg(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_ADD_DLG, pParent), m_strName(_T("")), m_strSex(_T("")), m_nAge(0), m_strDeparment(_T("")), m_strJob(_T("")), m_fSalary(0)
{}CAddDlg::~CAddDlg()
{
}void CAddDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Text(pDX, IDC_EDIT2, m_strName);DDX_Text(pDX, IDC_EDIT1, m_strSex);DDX_Text(pDX, IDC_EDIT3, m_nAge);DDX_Text(pDX, IDC_EDIT4, m_strDeparment);DDX_Text(pDX, IDC_EDIT5, m_strJob);DDX_Text(pDX, IDC_EDIT6, m_fSalary);
}BEGIN_MESSAGE_MAP(CAddDlg, CDialogEx)ON_BN_CLICKED(IDC_ADD, &CAddDlg::OnBnClickedAdd)END_MESSAGE_MAP()// CAddDlg 消息处理程序void CAddDlg::OnBnClickedAdd()
{UpdateData();EndDialog(IDOK);
}
// CModifyDlg 对话框
class CModifyDlg : public CDialogEx
{DECLARE_DYNAMIC(CModifyDlg)public:CModifyDlg(CWnd* pParent = nullptr); // 标准构造函数virtual ~CModifyDlg();// 对话框数据
#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_MODIFY_DLG };
#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持DECLARE_MESSAGE_MAP()
public:CString m_strName;CString m_strSex;int m_nAge;CString m_strDeparment;CString m_strJob;float m_fSalary;afx_msg void OnBnClickedModify();
};
// CModifyDlg.cpp: 实现文件
//#include "pch.h"
#include "SQL.h"
#include "CModifyDlg.h"
#include "afxdialogex.h"// CModifyDlg 对话框IMPLEMENT_DYNAMIC(CModifyDlg, CDialogEx)CModifyDlg::CModifyDlg(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_MODIFY_DLG, pParent), m_strName(_T("")), m_strSex(_T("")), m_nAge(0), m_strDeparment(_T("")), m_strJob(_T("")), m_fSalary(0)
{}CModifyDlg::~CModifyDlg()
{
}void CModifyDlg::DoDataExchange(CDataExchange* pDX)
{CDialogEx::DoDataExchange(pDX);DDX_Text(pDX, IDC_EDIT2, m_strName);DDX_Text(pDX, IDC_EDIT1, m_strSex);DDX_Text(pDX, IDC_EDIT3, m_nAge);DDX_Text(pDX, IDC_EDIT4, m_strDeparment);DDX_Text(pDX, IDC_EDIT5, m_strJob);DDX_Text(pDX, IDC_EDIT6, m_fSalary);
}BEGIN_MESSAGE_MAP(CModifyDlg, CDialogEx)ON_BN_CLICKED(IDC_MODIFY, &CModifyDlg::OnBnClickedModify)
END_MESSAGE_MAP()// CModifyDlg 消息处理程序void CModifyDlg::OnBnClickedModify()
{// TODO: 在此添加控件通知处理程序代码UpdateData();EndDialog(IDOK);
}
#pragma once
#include "mysql.h"
class CMySQL
{
public:CMySQL();~CMySQL();
public://连接数据库BOOL Connect(const char *host,const char *user,const char *passwd,const char *db,unsigned int port,const char *unix_socket,unsigned long client_flag);//执行SQL语句int Query(const char* str);//保存结果集MYSQL_RES* StoreResult();//检索结果集MYSQL_ROW FetchRow();
private:MYSQL* m_mysql;MYSQL_RES* m_mysql_res;
public:MYSQL_ROW m_mysql_row;};
#include "pch.h"
#include "CMySQL.h"#pragma comment(lib,"libmysql.lib")CMySQL::CMySQL()
{m_mysql = mysql_init(NULL);
}CMySQL::~CMySQL()
{
}BOOL CMySQL::Connect(const char * host, const char * user, const char * passwd, const char * db, unsigned int port, const char * unix_socket, unsigned long client_flag)
{if (NULL == mysql_real_connect(m_mysql, host, user, passwd, db, port, unix_socket, client_flag)){return FALSE;}return TRUE;}int CMySQL::Query(const char * str)
{return mysql_real_query( m_mysql,str,strlen(str));
}//保存结果集
MYSQL_RES * CMySQL::StoreResult()
{m_mysql_res=mysql_store_result(m_mysql);return m_mysql_res;
}MYSQL_ROW CMySQL::FetchRow()
{m_mysql_row=mysql_fetch_row(m_mysql_res);return m_mysql_row;
}