VC6连接MYSQL
2014-03-05 15:28
281 查看
(1)
vc6.0添加mysql #include 头文件: 工程->设置->C/C++ 分类那里选择Preprocessor(预处理器),然后在附加包含路径里写上:C:\Program Files\MySQL\MySQL Server 5.6\include就OK 了(路径是按照本机的MySQL安装目录而定)
(2).lib 对应的.dll也要拷贝到工程目录下。(libmysql.lib和libmysql.dll,试验项目名为MySQL_Connect)
(3)在编写程序时添加进去相应的lib库,使用:#pragma comment(lib, "libmysql.lib")
完整的例子如下:
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <mysql.h>
#pragma comment(lib, "libmysql.lib")
using namespace std;
MYSQL * conn;
int main()
{
char host[] = "localhost";
char username[] = "root";//管理员用户
char password[] = "zzy19921117";//密码
char database[] = "enterprise";//对应的数据库名字
MYSQL_RES * res_set;
MYSQL_ROW row;
unsigned int i, ret;
MYSQL_FIELD * field;
unsigned int num_fields;
//mysql_init(MYSQL *)
// return values: An initialized MYSQL* handle.
//NULL if there was insufficient memory to allocate a new object.
conn = mysql_init(NULL);
if(conn != NULL)
cout << "mysql_init success!" << endl;
else printf("mysql_init failed !\n");
ret = mysql_options(conn, MYSQL_SET_CHARSET_NAME, "gb2312");
if(ret == 0)
cout << "mysql_options success!" << endl;
else printf("mysql_options failed !\n");
if(mysql_real_connect(conn,
host,
username,
password,
database,
0, NULL, 0) != NULL)
cout << "mysql_real_connect success!" << endl;
else printf("mysql_real_connect failed !\n");
//printf("char set %s\n", mysql_character_set_name(conn));
//往指定的名为“enterprise”的数据库中的表vendor插入一条数据
mysql_query(conn, "insert into vendor values(6, 'wahaha', 'shenzhen', 'phone6')");
printf("insert affect %d sentences\n", mysql_affected_rows(conn));
//查询vendor数据,保存在conn中
if(mysql_query(conn,"SELECT * FROM vendor")) //查询成功返回0 failed here!
printf("mysql_query failed!\n");
res_set = mysql_store_result(conn); //失败返回NULL
if(res_set == NULL)
printf("res_set is null\n");
/*
while((field = mysql_fetch_field(res_set)))
{
printf("field name %s\n", field->name);
}
*/
//输出vendor表中的各属性
num_fields = mysql_num_fields(res_set);
for(i = 0; i < num_fields; i++)
{
field = mysql_fetch_field_direct(res_set, i);
printf("%s\t\t", field->name);
}
printf("\n");
//输出vendor表中的所有元组
while ((row = mysql_fetch_row(res_set)) != NULL)
{
for (i = 0; i < mysql_num_fields(res_set); i ++)
{
printf("%s\t\t",row[i] != NULL ? row[i] : "NULL");
}
printf("\n");
}
mysql_close(conn);
return 0;
}
PS:
若读者搞不懂代码中的MYSQL、MYSQL_RES 、MYSQL_ROW 、MYSQL_FIELD 等数据结构,可以登录MySQL官网搜索
http://dev.mysql.com/doc/refman/5.1/en/c-api-data-structures.html
最后,本文乃参考毛毛虫的文章,十分感谢!
附加参考链接:
http://hi.baidu.com/fdontyiqzcbcuvr/item/dbd28b1fb9c4c65c2a3e2209?qq-pf-to=pcqq.c2c
vc6.0添加mysql #include 头文件: 工程->设置->C/C++ 分类那里选择Preprocessor(预处理器),然后在附加包含路径里写上:C:\Program Files\MySQL\MySQL Server 5.6\include就OK 了(路径是按照本机的MySQL安装目录而定)
(2).lib 对应的.dll也要拷贝到工程目录下。(libmysql.lib和libmysql.dll,试验项目名为MySQL_Connect)
(3)在编写程序时添加进去相应的lib库,使用:#pragma comment(lib, "libmysql.lib")
完整的例子如下:
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <mysql.h>
#pragma comment(lib, "libmysql.lib")
using namespace std;
MYSQL * conn;
int main()
{
char host[] = "localhost";
char username[] = "root";//管理员用户
char password[] = "zzy19921117";//密码
char database[] = "enterprise";//对应的数据库名字
MYSQL_RES * res_set;
MYSQL_ROW row;
unsigned int i, ret;
MYSQL_FIELD * field;
unsigned int num_fields;
//mysql_init(MYSQL *)
// return values: An initialized MYSQL* handle.
//NULL if there was insufficient memory to allocate a new object.
conn = mysql_init(NULL);
if(conn != NULL)
cout << "mysql_init success!" << endl;
else printf("mysql_init failed !\n");
ret = mysql_options(conn, MYSQL_SET_CHARSET_NAME, "gb2312");
if(ret == 0)
cout << "mysql_options success!" << endl;
else printf("mysql_options failed !\n");
if(mysql_real_connect(conn,
host,
username,
password,
database,
0, NULL, 0) != NULL)
cout << "mysql_real_connect success!" << endl;
else printf("mysql_real_connect failed !\n");
//printf("char set %s\n", mysql_character_set_name(conn));
//往指定的名为“enterprise”的数据库中的表vendor插入一条数据
mysql_query(conn, "insert into vendor values(6, 'wahaha', 'shenzhen', 'phone6')");
printf("insert affect %d sentences\n", mysql_affected_rows(conn));
//查询vendor数据,保存在conn中
if(mysql_query(conn,"SELECT * FROM vendor")) //查询成功返回0 failed here!
printf("mysql_query failed!\n");
res_set = mysql_store_result(conn); //失败返回NULL
if(res_set == NULL)
printf("res_set is null\n");
/*
while((field = mysql_fetch_field(res_set)))
{
printf("field name %s\n", field->name);
}
*/
//输出vendor表中的各属性
num_fields = mysql_num_fields(res_set);
for(i = 0; i < num_fields; i++)
{
field = mysql_fetch_field_direct(res_set, i);
printf("%s\t\t", field->name);
}
printf("\n");
//输出vendor表中的所有元组
while ((row = mysql_fetch_row(res_set)) != NULL)
{
for (i = 0; i < mysql_num_fields(res_set); i ++)
{
printf("%s\t\t",row[i] != NULL ? row[i] : "NULL");
}
printf("\n");
}
mysql_close(conn);
return 0;
}
PS:
若读者搞不懂代码中的MYSQL、MYSQL_RES 、MYSQL_ROW 、MYSQL_FIELD 等数据结构,可以登录MySQL官网搜索
http://dev.mysql.com/doc/refman/5.1/en/c-api-data-structures.html
最后,本文乃参考毛毛虫的文章,十分感谢!
附加参考链接:
http://hi.baidu.com/fdontyiqzcbcuvr/item/dbd28b1fb9c4c65c2a3e2209?qq-pf-to=pcqq.c2c
相关文章推荐
- mysql 模拟四
- mysqldump 导出数据文件,是乱码,何解?
- Mysql如何选择存储引擎
- mysql游标循环的使用
- 查看mysql表结构和表创建语句的方法(转)
- Mysql通过EXPLAIN 分析低效SQL 的执行计划
- MySQL VS PostgreSQL:该选择哪个开源数据库?
- mysql模拟题三
- mysql 本地连接失败
- mysql模拟题二
- mysql 添加用户以及授权
- MySQLdb捕捉警告信息
- mysql 模拟试题一
- mysql分表的3种方法
- Mysql分表和分区的区别
- EXPLAIN语句对于MySQLdb捕获异常的影响
- Mysql通过show status 命令了解各种SQL 的执行频率
- Warning: World-writable config file '/etc/mysql/my.cnf' is ignored
- MySql语句大全
- mysql问题积累示例