vc++与MySQL数据库的连接

3 篇文章 0 订阅

转至http://blog.csdn.net/nuptboyzhb/article/details/8043091                                      

                                    vc++与MySQL数据库的连接

1.MySQL数据库的安装

 你可以从MySQL的官网上或者从如下地址下载MySQL的数据库安装包(http://download.csdn.net/detail/nuptboyzhb/4619847)。本文以mysql-5.0.27-win32为例。下载完之后解压安装。注意:在安装的过程中,选择安装“完全版”(complete),不要选择默认的“典型”。否者,没有c++相关的连接库。然后一直点next即可。安装过程中让你注册,你就按部就班的做就行了。安装完之后的文件目录如下:

 [Image]


2.数据库的建立
 你可以直接用MySQL的命令行窗口去建立一个数据库和一个表。但是,我强烈推荐你用可视化的工具(你可以去搜一下)。在这里,我之前安装过wamp5就直接用网页版的phpmyadmin工具。如果你安装过wamp5,直接在浏览器中输入:http://localhost/phpmyadmin/即可。然后我们新建一个名为testdb的数据库再在其中新建一个表:name_table.然后新增数据:zhb 22studentsnjupt

 [Image][Image]



3.VC++6.0的配置
本文以经典的vc++6.0为例。(当然,VS2005 2008 2010等,也是这样配置)。打开vc++6.0,工具->选项->目录(选项卡),在其Include files添加MySQL的include路径。如我的MySQL的include文件夹的路径为:C:\Program Files\MySQL\MySQL Server 5.0\include。切换下拉框,选择Library files,添加MySQL的lib路径。如我的为:C:\Program Files\MySQL\MySQL Server 5.0\lib\opt
4.编程连接数据库并查询

[c++ codes]

[cpp]  view plain copy
  1. #include <windows.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <mysql.h>   
  6. #include <iostream>  
  7. #pragma comment(lib,"libmysql.lib")//连接MysQL需要的库  
  8. using namespace std;  
  9. int main()  
  10. {  
  11.     const char user[] = "root";         //username  
  12.     const char pswd[] = "*********";    //password  
  13.     const char host[] = "localhost";    //or"127.0.0.1"  
  14.     const char table[] ="testdb";       //database  
  15.     unsigned int port = 3306;           //server port          
  16.     MYSQL myCont;  
  17.     MYSQL_RES *result;  
  18.     MYSQL_ROW sql_row;  
  19.     MYSQL_FIELD *fd;  
  20.     char column[32][32];  
  21.     int res;  
  22.     mysql_init(&myCont);  
  23.     if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))  
  24.     {  
  25.         cout<<"connect succeed!"<<endl;  
  26.         mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文  
  27.         res=mysql_query(&myCont,"select * from name_table");//查询  
  28.         if(!res)  
  29.         {  
  30.             result=mysql_store_result(&myCont);//保存查询到的数据到result  
  31.             if(result)  
  32.             {  
  33.                 int i,j;  
  34.                 cout<<"number of result: "<<(unsigned long)mysql_num_rows(result)<<endl;  
  35.                 for(i=0;fd=mysql_fetch_field(result);i++)//获取列名  
  36.                 {  
  37.                     strcpy(column[i],fd->name);  
  38.                 }  
  39.                 j=mysql_num_fields(result);  
  40.                 for(i=0;i<j;i++)  
  41.                 {  
  42.                     printf("%s\t",column[i]);  
  43.                 }  
  44.                 printf("\n");  
  45.                 while(sql_row=mysql_fetch_row(result))//获取具体的数据  
  46.                 {  
  47.                     for(i=0;i<j;i++)  
  48.                     {  
  49.                         printf("%s\n",sql_row[i]);  
  50.                     }  
  51.                     printf("\n");  
  52.                 }  
  53.             }  
  54.         }  
  55.         else  
  56.         {  
  57.             cout<<"query sql failed!"<<endl;  
  58.         }  
  59.     }  
  60.     else  
  61.     {  
  62.         cout<<"connect failed!"<<endl;  
  63.     }  
  64.     if(result!=NULL) mysql_free_result(result);//释放结果资源  
  65.     mysql_close(&myCont);//断开连接  
  66.     return 0;  
  67. }  


实验结果

[Image]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值