一、创建项目如下图:
二、配置编译器
1、工具—》选项
2、如图
3、如图:
3、项目附加配置
添加libmysql.lib mysqlclient.lib mysys.lib zlib.lib库文件,中间以空格隔开
三、内容
里面有注释,在这里不做解释!
#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
==========
五、程序运行结果
下面为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
执行结构一致!示例程序成功!
如有不明之处,请留言共同商讨!