VS2010 中 C++ 操作 MySQL

来源:互联网 发布:商务数据分析是什么 编辑:程序博客网 时间:2024/06/04 18:06

前提: MySQL正确安装,已创建好数据库与表




本程序中, 表结构为:



1.创建一个基于对话框的MFC程序

环境设置:


在工程中,添加需要包含的头与库文件





建议将“libmySQL.lib、libmySQL.dll”拷到你所建的工程的目录下。



2.放置控件并设置相应的变量


3.功能实现

**Dlg.h  头文件中

包含两个头文件

#include "mysql.h" 

#include "winsock.h"


将控件添加相应的变量

CString m_strId;         // ID编辑框对应的变量

CString m_strName;    // 姓名编辑框对应的变量

CString m_strAddr;      // 地址编辑框对应的变量

CListCtrl m_wndLstView;// List 控件


MYSQL mysql;           //数据库连接句柄


#pragma comment(lib,"libmySQL.lib")  // 导入lib



**Dlg.cpp 源文件中


A. 在 BOOL CConnMySQLDlg::OnInitDialog() 函数中,将List控件初始化

---------------------------------------------------------------------------------

// IDC_LST_VIEW 控件设置

m_wndLstView.SetExtendedStyle(m_wndLstView.GetExtendedStyle() |\

LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);


// 对话框宽度

RECT rcList;

m_wndLstView.GetWindowRect(&rcList);

int nLstWid = rcList.right - rcList.left;


// 设置表头

m_wndLstView.InsertColumn(0, _T("ID"), LVCFMT_LEFT, nLstWid/4,-1);

m_wndLstView.InsertColumn(1, _T("姓名"), LVCFMT_LEFT, nLstWid/3-10,-1);

m_wndLstView.InsertColumn(2, _T("地址"), LVCFMT_LEFT, nLstWid/3+40,-1);

---------------------------------------------------------------------------------


进行进行数据库连接

---------------------------------------------------------------------------------

// 连接数据库

mysql_init(&mysql);


if (!mysql_real_connect(&mysql, "localhost", "root", "yulinxx", "xxdb", 3306, NULL, 0))

{

     AfxMessageBox(_T("连接错误!\n"));

return FALSE;

}

else

{

     OnBnClickedBtnView();// 调用查看按钮,如果数据库有数据,则在列表显示数据

}


B. 添加按钮的实现

---------------------------------------------------------------------------------


// 添加按钮

void CConnMySQLDlg::OnBnClickedBtnAdd()

{

UpdateData(TRUE);

if (m_strId.IsEmpty())

{

AfxMessageBox(_T("请填写ID\n"));

}

CString strSql;

strSql.Format(_T("insert into xxtable(id, name, address) values(\'%s\', \'%s\', \'%s\')"),

m_strId, m_strName, m_strAddr);


CStringA strSQL;

strSQL = strSql;

char* pCh = strSQL.GetBuffer(strSQL.GetLength());


if(mysql_real_query(&mysql,pCh,(UINT)strSql.GetLength())!=0)

{

AfxMessageBox(_T("添加失败\n"));

}


OnBnClickedBtnView();// 调用查看按钮

}


---------------------------------------------------------------------------------


C. 删除按钮的实现

---------------------------------------------------------------------------------

// 删除按钮

void CConnMySQLDlg::OnBnClickedBtnDel()

{

UpdateData(TRUE);


// 从ID, Name, Address 分别判断,优先删除前面的条件

CString strSql;

if (!m_strId.IsEmpty())

{

strSql.Format(_T("delete from xxtable where id=\'%s\'"),m_strId);

}

else if (!m_strName.IsEmpty())

{

strSql.Format(_T("delete from xxtable where name=\'%s\'"),m_strName);

}

else

{

strSql.Format(_T("delete from xxtable where address=\'%s\'"),m_strAddr);

}


CStringA strSQL;

strSQL = strSql;

char* pCh = strSQL.GetBuffer(strSQL.GetLength());


if(mysql_real_query(&mysql,pCh,(UINT)strSql.GetLength())!=0)

{

AfxMessageBox(_T("删除失败"));

}


OnBnClickedBtnView();// 调用查看按钮

}

---------------------------------------------------------------------------------


D. 查看按钮的实现

---------------------------------------------------------------------------------


// 查看按钮

void CConnMySQLDlg::OnBnClickedBtnView()

{

  m_wndLstView.DeleteAllItems();// 清空列表

  char *ch_query;

  ch_query="select * from xxtable";

  if(mysql_real_query(&mysql, ch_query, (UINT)strlen(ch_query))!=0)

  {

       AfxMessageBox(_T("查询数据库失败")); 

   }

CString str;

CStringA strA;


MYSQL_RES *result;

MYSQL_ROW row;

if(!(result=mysql_use_result(&mysql)))

{

     AfxMessageBox(_T("读取数据集失败")); 

}


// 查询结果插入列表

int i = 0;

while(row = mysql_fetch_row(result))

{

    strA = row[0];

    str = strA;

    m_wndLstView.InsertItem(i, str);

    strA = row[1];

    str = strA;

    m_wndLstView.SetItemText(i, 1, str);

    strA = row[2];

    str = strA;

    m_wndLstView.SetItemText(i, 2, str);

    i++;

 }

  mysql_free_result(result);

}

---------------------------------------------------------------------------------


E.数据库的关闭

在***Dlg类中添加一个OnDestroy函数

---------------------------------------------------------------------------------

void CConnMySQLDlg::OnDestroy()

{

    CDialogEx::OnDestroy();


    mysql_close(&mysql); // 关闭

}

---------------------------------------------------------------------------------




4.实现过程此过程遇到的问题及错误


由于是Win7 64位  + 32位VS,在最操作数据库时一直失败,只能连接,不能对数据库进行操作.

解决方法: 工程,右键->属性

在 右上角 "配置管理器", 新建 X64 即可




例如MySQL,sqlite3,Lua等数据库的SQL语句字符串必须使用char*类型的。

所以注意程序中字符串的转换

源码下载: http://pan.baidu.com/share/link?shareid=154984&uk=3239860864

程序比较粗劣,见谅



http://hi.baidu.com/yulinxx_/item/a7553c079b7c93983c42e237

0 0