mysql连接时报2005_Visual Studio 2005 连接mysql数据库实例

一、创建项目如下图:

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

二、配置编译器

1、工具—》选项

a4c26d1e5885305701be709a3d33442f.png

2、如图

a4c26d1e5885305701be709a3d33442f.png

3、如图:

a4c26d1e5885305701be709a3d33442f.png

3、项目附加配置

添加libmysql.lib mysqlclient.lib mysys.lib zlib.lib库文件,中间以空格隔开

a4c26d1e5885305701be709a3d33442f.png

三、内容

里面有注释,在这里不做解释!

#include "stdafx.h"

#include "winsock2.h"

#include

#include

#include

#include

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

char *host = "localhost";//数据库IP

char *user =

"root";//数据库账号

char *pass =

"请修改为自己的密码 ";//数据库密码

char *db =

"my_database";//当前数据库

//数据类型声明

MYSQL

*sock;//数据库连接指针声明

MYSQL_RES

*results;//查询结果集声明

MYSQL_ROW

record;//查询结果行声明

sock =

mysql_init(0);//初始化指针

if

(sock)

cout << "数据库程序初始化成功!"

<< endl;

else

{

cout << "数据库程序初始化失败!"

<< mysql_error(sock)

<< endl;

}

//connection

if

(mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))

cout << "数据库连接成功!"

<< endl;

else

{

cout << "数据库连接失败: "

<< mysql_error(sock)

<< endl;

}

mysql_set_character_set(sock,"gb2312");//设置当前字符为gb2312,支持中文

//connection

character set

cout

<< "当前字符集为: "

<< mysql_character_set_name(sock)

<< endl;

//执行查询

if(mysql_query(sock,"select * from my_table"))

{

cout<

results=mysql_store_result(sock);

printf("aid\t id\t first\tsec\n");

while(record

= mysql_fetch_row(results))

{

printf("%s\t%s\t%s\t%s\n",record[0],record[1],record[2],record[3]);

}

mysql_free_result(results);//释放结果

//关闭连接

mysql_close(sock);

return EXIT_SUCCESS;

return 0;

}

四、编译

按F7执行编译

1>------ Build started: Project: first_mysql,

Configuration: Debug Win32 ------

1>Compiling...

1>stdafx.cpp

1>Compiling...

1>first_mysql.cpp

1>Compiling manifest to resources...

1>Linking...

1>LINK : D:\My Documents\Visual Studio

2005\Projects\first_mysql\Debug\first_mysql.exe not found or not

built by the last incremental link; performing full link

1>Embedding manifest...

1>Build log was saved at "file://d:\My Documents\Visual Studio

2005\Projects\first_mysql\first_mysql\Debug\BuildLog.htm"

1>first_mysql - 0 error(s), 0 warning(s)

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped

==========

五、程序运行结果

a4c26d1e5885305701be709a3d33442f.png

下面为mysql查询结果:

mysql> use my_database;

Database changed

mysql> select * from

my_table;

+-----+------+-------+----------+

| aid | id | first |

sec |

+-----+------+-------+----------+

| 1

| 1 |

世界 | ID为奇数 |

| 2

| 5 |

中国 | ID为奇数 |

| 3

| 3 |

中国 | ID为奇数 |

| 4

| 2 |

124 | ID为偶数 |

| 5

| 4 |

时候 | ID为偶数 |

| 6

| 6 |

235 | ID为偶数 |

+-----+------+-------+----------+

6 rows in set (0.06 sec)

六、附件

1、my_table的数据结构

mysql> desc my_table;

+-------+--------------+------+-----+---------+----------------+

| Field |

Type | Null | Key | Default |

Extra |

+-------+--------------+------+-----+---------+----------------+

| aid |

int(5) | NO | PRI |

NULL |

auto_increment |

| id |

int(3) | YES | | NULL | |

| first | varchar(100) | YES | MUL |

NULL | |

| sec |

varchar(30) | YES | | NULL | |

+-------+--------------+------+-----+---------+----------------+

4 rows in set (0.08 sec)

2、创建表语句

CREATE TABLE `my_table` (

`aid` int(5) NOT NULL AUTO_INCREMENT,

`id` int(3) DEFAULT NULL,

`first` varchar(100) DEFAULT NULL,

`sec` varchar(30) DEFAULT NULL,

PRIMARY KEY (`aid`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=gb2312

执行结构一致!示例程序成功!

如有不明之处,请留言共同商讨!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值