VS2008中MySQL数据库操作实例
2015-10-16 19:23
381 查看
环境设置:
安装完MySQL之后,将安装目录中的include目录下的libmysql.lib文件拷到VS2008安装目录中的VC\lib\下,然后在 项目-选项-c/c++-常规 中的附加包含目录以及 链接器-常规 中的附加库目录中加入“c:\MySQL\include\”,并且在 链接器-输入 中的附加依赖项内添加“libmysql.lib”,这样即可使编译器找到mysql.h头文件,并可在程序中使用c语言的mysql API来操作数据库。(如果MySQL安装目录中无include目录,可到MySQL官网下载并安装MySQL
connector for C,并修改include目录路径)
代码示例:
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <iostream>
using namespace std;
int main()
{
const char user[] = "root"; //username
const char pswd[] = "root"; //password
const char host[] = "localhost"; //or"127.0.0.1"
const char table[] = "test"; //database
unsigned int port = 3306; //server port
MYSQL myCont;
MYSQL_RES *result;
MYSQL_ROW sql_row;
MYSQL_FIELD *fd;
char column[32][32];
int res;
mysql_init(&myCont);
if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))
{
cout<<"connect succeed!"<<endl;
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
res=mysql_query(&myCont,"select * from samples");//查询
if(!res)
{
result=mysql_store_result(&myCont);//保存查询到的数据到result
if(result)
{
int i,j;
cout<<"number of result: "<<(unsigned long)mysql_num_rows(result)<<endl;
for(i=0;fd=mysql_fetch_field(result);i++)//获取列名
{
strcpy(column[i],fd->name);
}
j=mysql_num_fields(result);
for(i=0;i<j;i++)
{
printf("%s\t",column[i]);
}
printf("\n");
while(sql_row=mysql_fetch_row(result))//获取具体的数据
{
for(i=0;i<j;i++)
{
printf("%s\n",sql_row[i]);
}
printf("\n");
}
}
}
else
{
cout<<"query sql failed!"<<endl;
}
}
else
{
cout<<"connect failed!"<<endl;
}
if(result!=NULL) mysql_free_result(result);//释放结果资源
mysql_close(&myCont);//断开连接
return 0;
}
安装完MySQL之后,将安装目录中的include目录下的libmysql.lib文件拷到VS2008安装目录中的VC\lib\下,然后在 项目-选项-c/c++-常规 中的附加包含目录以及 链接器-常规 中的附加库目录中加入“c:\MySQL\include\”,并且在 链接器-输入 中的附加依赖项内添加“libmysql.lib”,这样即可使编译器找到mysql.h头文件,并可在程序中使用c语言的mysql API来操作数据库。(如果MySQL安装目录中无include目录,可到MySQL官网下载并安装MySQL
connector for C,并修改include目录路径)
代码示例:
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <iostream>
using namespace std;
int main()
{
const char user[] = "root"; //username
const char pswd[] = "root"; //password
const char host[] = "localhost"; //or"127.0.0.1"
const char table[] = "test"; //database
unsigned int port = 3306; //server port
MYSQL myCont;
MYSQL_RES *result;
MYSQL_ROW sql_row;
MYSQL_FIELD *fd;
char column[32][32];
int res;
mysql_init(&myCont);
if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))
{
cout<<"connect succeed!"<<endl;
mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文
res=mysql_query(&myCont,"select * from samples");//查询
if(!res)
{
result=mysql_store_result(&myCont);//保存查询到的数据到result
if(result)
{
int i,j;
cout<<"number of result: "<<(unsigned long)mysql_num_rows(result)<<endl;
for(i=0;fd=mysql_fetch_field(result);i++)//获取列名
{
strcpy(column[i],fd->name);
}
j=mysql_num_fields(result);
for(i=0;i<j;i++)
{
printf("%s\t",column[i]);
}
printf("\n");
while(sql_row=mysql_fetch_row(result))//获取具体的数据
{
for(i=0;i<j;i++)
{
printf("%s\n",sql_row[i]);
}
printf("\n");
}
}
}
else
{
cout<<"query sql failed!"<<endl;
}
}
else
{
cout<<"connect failed!"<<endl;
}
if(result!=NULL) mysql_free_result(result);//释放结果资源
mysql_close(&myCont);//断开连接
return 0;
}
相关文章推荐
- mysql故障
- haproxy实现mysql slave负载均衡
- MySQL存储引擎MyISAM和InnoDB的对比
- mysql 左外连接 右外链接 内连接
- mac 下的mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock
- mysql创建外链失败1005错误解决方法
- mysql数据库时间字段CURRENT_TIME问题
- WINDOWS下更改MYSQL数据路径(datadir)后服务启动1067解决不能改变mysql数据库存储位置
- 几个常见的MySQL性能测试工具
- mysql安装及配置服务
- MySQL找出表中重复字段的内容
- MySQL SQL语句优化的10条建议
- MySql 查询优化
- 查询练习mysql
- 【Mysql学习笔记】DDL、DML、DCL区别
- mysql 函数大全
- MYSQL SQL表操作
- 超级简单的mysql主从数据库配置攻略以及错误处理
- Ubuntu下mysql数据库自动备份脚本
- MySQL(17):Select-union(联合查询)使用注意事项