vc mysql_vc6.0连接mysql数据库

一、MySQL的安装

Mysql的安装去官网下载就可以。。。最新的是5.7版本。。

二、VC6.0的设置

(1)打开VC6.中选0 工具栏Tools菜单下的Options选项,在Directories的标签页中右边的“Show directories for:”下拉列表中“Includefiles”,然后在中间列表框中添加你本地安装MySQL的include目录路径。如图:

0dface3e7f7db721bba2d35d6b453cf5.png

(2)在上面说到的“Show directories for:”下拉列表中选中“Library files”,然后添加你本地安装MySQL的Lib目录路径。如图:

8be20645f960a7097eb014b4f25fb207.png

**这里要说明一下:细心的人会发现我的这个目录和上一个图中的不一样,这是因为这个错误:libmysql.lib : fatal error LNK1113: invalid machine 无效的服务器

这是因为vc开发的是32位的程序,而mysql数据库是64位导致的,你用32位的程序去操作64位的数据库肯定会出错,我在下一篇博文中将详细说明怎么解决。

(3)在“Project settings->Link:Object/library modules”里面添加“libmysql.lib”。

3db08cc6e9b8ef6b31dac7f0938d8233.png

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

这两个文件在D:\Mysql\lib目录下。

三、编程实现

1. 一个简单的小程序,看看是否能连接成功。。。

#include

#include

#include

int main()

{

MYSQL mysql;

mysql_init(&mysql); //初始化mysql结构

if(!mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0))

printf("\n连接数据库时发生错误!\n");

else

printf("\n连接数据库成功!\n");

mysql_close(&mysql); //释放数据库

return 0;

}

mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0)//myuser是我的用户名,“123456”是密码,“student_db”是数据库,3306是端口号

2.实现查询小程序

// test.cpp : Defines the entry point for the console application.

//

#include

#include

#include "StdAfx.h"

#include

#include

#include

#include

using namespace std;

//不需要单步调试的就注释掉

//#define STEPBYSTEP

void pause(){

#ifdef STEPBYSTEP

system("pause");

#endif

}

void writeToFile(const char *s)

{

FILE *fp=fopen("info.txt","rw");

fprintf(fp,s);

fclose(fp);

}

/* int main()

{

MYSQL mysql;

mysql_init(&mysql); //初始化mysql结构

if(!mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0))

printf("\n连接数据库时发生错误!\n");

else

printf("\n连接数据库成功!\n");

mysql_close(&mysql); //释放数据库

return 0;

}*/

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

cout<

pause();

MYSQL mysql;

if(0==mysql_library_init(0,NULL,NULL))

{

cout<

}else{

cout<

return -1;

}

pause();

if(NULL!=mysql_init(&mysql))

{

cout<

}else{

cout<

return -1;

}

pause();

if(0==mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"gb2312"))

{

cout<

}else{

cout<

return -1;

}

pause();

if(NULL!=mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0))

{

cout<

}else{

cout<

return -1;

}

pause();

string sql;

sql="select * from sgroup";

MYSQL_RES *result=NULL;

if(0==mysql_query(&mysql,sql.c_str()))

{

cout<

result=mysql_store_result(&mysql);

int rowcount=mysql_num_rows(result);

cout<

unsigned int fieldcount=mysql_num_fields(result);

MYSQL_FIELD *field=NULL;

for(unsigned int i=0;i

{

field=mysql_fetch_field_direct(result,i);

cout<name<

}

cout<

MYSQL_ROW row=NULL;

row=mysql_fetch_row(result);

while(NULL!=row)

{

for(int i=0;i

cout<

}

cout<

row=mysql_fetch_row(result);

}

}else{

cout<

mysql_close(&mysql);

return -1;

}

pause();

/*sql="drop table user_info";

if(0==mysql_query(&mysql,sql.c_str()))

{

cout<

}else{

cout<

mysql_close(&mysql);

return -1;

} */

mysql_free_result(result);

mysql_close(&mysql);

mysql_server_end();

system("pause");

return 0;

}

运行结果:

d391cc948d439e70285378b7c9e1ac68.png

至此连接成功。。哈哈。。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值