您的位置:首页 > 数据库 > MySQL

VC连接Mysql及开发详细指南

2013-11-27 14:02 197 查看
VC与MYSQL连接
最近要用VC连接WEB上的MYSQL,在网上找了很多资料.都不是非常的详细。

所以今天,我就把我自已怎样一步步连接MYSQL的步骤,以及语句等的写法一起写出来。

一是为了自已备忘,同时也希望能对需要的朋友有帮助.

准备工作:

建议:以下最好都用方法1,简单易用,也可以防止以后使用麻烦。

一、mysql.h头文件问题。

因为新在VC工程,在里面要包含这样的头文件
#include "winsock.h"
#include "mysql.h"

所以:
要把mysql.h复制到vc的安装目录的include目录下
方法1:
mysql.h在你mysql的安装目录下的include里面如:mysql\include

方法2:
把这个头文件添加到你的目录里面,或者在VC(我用的是Visual c++6.0)的菜单栏
点击progect-->setting-->在出现的对话框里面选择c/c++属性页-->选择preprocessor
--->在下面的additional include directories文本框里面填入你的mysql.h目录如:
D:\DataBase\mysql\include

建议:

把mysql.h和其他在mysql\include的所以.h的文件都复制到VS6的安装目录里,如我的是:
我visula studio是安装在D:\Program Files\下
下面是详细的目录地址:

D:\Program Files\Microsoft Visual Studio\VC98\Include

二、libmysql.lib链接库问题:

方法1:
把libmysql.lib(在mysql的安装目录下,搜索下就能找到)也复制到这个目录下(D:\Program Files\Microsoft Visual Studio\VC98\Include),要不连接会出错。

如果编译连接时还是出错。就把libmysql.lib复制到你源程序的目录中

方法2:

libmysql.lib 在安装目录下的lib\debug目录下

点击progect-->setting-->在出现的对话框里面选择link属性页-->选择input选项
-->在Object/library modules下面添加libmysql.lib
-->在下面的additional include directories文本框里面输入你的lib的目录
如:\DataBase\mysql\lib\debug

建议使用方法1

下面是做示例工程。

一、新建一个VC-MFC(exe)工程,如取名为:odbc,

第一步选对话框,
第二第三步及以下选默认的选项一直到结束。

二、在ODBCDlg.h中加入头文件。

#include "winsock.h"
#include "mysql.h"
三、新建一个按钮button1。添加成员变量,及单击的消息。

在ODBCDlg.cpp如下代码:
void CODBCDlg::OnButton1()
{
// TODO: Add your control notification handler code here
char *query;
int t,r;
MYSQL_RES *res;
MYSQL_ROW row;

m_mysql = mysql_init(NULL);
if (!mysql_real_connect(m_mysql,"localhost","databasename",
"yourdatabase-password","yourtable",0,NULL,0))
{
CString errors =mysql_error(m_mysql);
MessageBox(errors,"´íÎó");

}

query="select text from yourtable";

t=mysql_real_query(m_mysql,query,(unsigned int) strlen(query));
if(t)
{

MessageBox(mysql_error(m_mysql));
}

res=mysql_use_result(m_mysql);

row=mysql_fetch_row(res);
CString sumstr;
for(r=0;r<=mysql_field_count(m_mysql);r++)
{
if(row<0)
{
MessageBox("error");
}
for(t=0;t<mysql_num_fields(res);t++)
{
CString Query = row[t];
sumstr+=Query+" ";

}

}
MessageBox(sumstr);
mysql_close(m_mysql);

}
编译运行。

以下是网上找的另一段连接代码,没有测试过是否可用。

挺久的了,随便写的测试
#include "winsock.h"
#include "mysql.h"

int main(int argc, char* argv[])
{

MYSQL * con = mysql_init((MYSQL*) 0);
LPSTR host = "***";
LPSTR user,psw,dbname,;
if ( con !=NULL
&& mysql_real_connect(con,host,user,psw,dbname,3306,NULL,0) )
{
if (!mysql_select_db(con,dbname))
{
printf("Select successfully the database!\n");
con ->reconnect = 1;
if (!mysql_query(con,"select * from table_name"))
{
printf("Show table---orders:\n");
MYSQL_RES * recordSet = mysql_store_result(con);
long j = mysql_num_fields(recordSet);
long i = (long)mysql_num_rows(recordSet);
//get RecordSet of fields
MYSQL_FIELD * field = mysql_fetch_fields(recordSet);
for(int l=0;l<j;l++)
{
printf("%s ",field[l].name);
}
printf("\n");

// get all the row of table---boothinfo
MYSQL_ROW row;
while( row = mysql_fetch_row(recordSet))
{
//get the number of fields

for(int l=0 ; l< j;l++)
{
if(row[l]==NULL || !strlen(row[l]))
printf("NULL ");
else
printf("%s ",row[l]);
}
printf("\n");
}
}
}
else
{
printf("Unable to select the databas!\n");
return 0;
}
return 1;
}
else
{
printf("can't connect to the mysql server!\n");
printf("Errors: %s",mysql_error(con));
return -1;
}
mysql_close(con);
}来自: http://hi.baidu.com/piaoshi111/blog/item/f832c200394271db267fb563.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: