zoukankan      html  css  js  c++  java
  • VC++6.0连接MySQL数据库(MySQL API)

    一、MySQL的安装

       Mysql的安装去官网下载就可以。。。最新的是5.7版本。。

    二、VC6.0的设置

    (1)打开VC6.中选0 工具栏Tools菜单下的Options选项,在Directories的标签页中右边的“Show directories for:”下拉列表中“Includefiles”,然后在中间列表框中添加你本地安装MySQL的include目录路径。如图:

       

    (2)在上面说到的“Show directories for:”下拉列表中选中“Library files”,然后添加你本地安装MySQL的Lib目录路径。如图:

       

       **这里要说明一下:细心的人会发现我的这个目录和上一个图中的不一样,这是因为这个错误:libmysql.lib : fatal error LNK1113: invalid machine 无效的服务器

       这是因为vc开发的是32位的程序,而mysql数据库是64位导致的,你用32位的程序去操作64位的数据库肯定会出错,我在下一篇博文中将详细说明怎么解决。

    (3)在“Project settings->Link:Object/library modules”里面添加“libmysql.lib”。
            

    (4)建议将“libmySQL.lib、libmySQL.dll”拷到你所建的工程的目录下。

          这两个文件在D:Mysqllib目录下。

       三、编程实现

       1. 一个简单的小程序,看看是否能连接成功。。。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #include <stdio.h>
    #include <windows.h>
    #include <mysql.h>
     
    int main()
    {
     
           MYSQL mysql;
           mysql_init(&mysql); //初始化mysql结构
     
           if(!mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0))
                  printf(" 连接数据库时发生错误! ");
           else
                  printf(" 连接数据库成功! ");
     
           mysql_close(&mysql); //释放数据库
       
           return 0;
    }

      mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0)//myuser是我的用户名,“123456”是密码,“student_db”是数据库,3306是端口号

      2.实现查询小程序

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    // test.cpp : Defines the entry point for the console application.
    //
    #include <stdio.h>
    #include <windows.h>
    #include "StdAfx.h"
     
    #include <winsock.h> 
    #include <iostream> 
    #include <string> 
    #include <mysql.h> 
    using namespace std; 
    //不需要单步调试的就注释掉 
    //#define STEPBYSTEP 
       
    void pause(){ 
       
        #ifdef STEPBYSTEP 
            system("pause"); 
        #endif 
    void writeToFile(const char *s) 
       
         FILE *fp=fopen("info.txt","rw"); 
         fprintf(fp,s); 
         fclose(fp); 
       
     /* int main()
     
    {
     
           MYSQL mysql;
           mysql_init(&mysql); //初始化mysql结构
     
           if(!mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0))
                  printf(" 连接数据库时发生错误! ");
           else
                  printf(" 连接数据库成功! ");
     
           mysql_close(&mysql); //释放数据库
       
           return 0;
    }*/
    int main(int argc, char* argv[]){ 
       
        cout<<"start...."<<endl; 
        pause(); 
        MYSQL mysql; 
        if(0==mysql_library_init(0,NULL,NULL)) 
        
            cout<<"mysql_library_init succeed"<<endl; 
       
        }else
            cout<<"mysql_library_init failed"<<endl; 
            return -1; 
        
        pause(); 
        if(NULL!=mysql_init(&mysql)) 
        
       
            cout<<"mysql_init succeed"<<endl; 
        }else
            cout<<"mysql_init failed"<<endl; 
            return -1; 
        
        pause(); 
        if(0==mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"gb2312")) 
        
       
            cout<<"mysql_option succeed"<<endl; 
        }else
            cout<<"mysql_option failed"<<endl; 
            return -1; 
        
        pause(); 
       
        if(NULL!=mysql_real_connect(&mysql,"localhost","myuser","123456","student_db",3306,NULL,0)) 
        
       
            cout<<"mysql_real_connect succeed"<<endl; 
        }else
            cout<<"mysql_real_connect failed"<<endl; 
            return -1; 
        
        pause(); 
        string sql; 
         
        sql="select * from sgroup"
        MYSQL_RES *result=NULL; 
        if(0==mysql_query(&mysql,sql.c_str())) 
        
       
                cout<<"mysql_query select succeed"<<endl; 
                result=mysql_store_result(&mysql); 
                int rowcount=mysql_num_rows(result); 
                cout<<"row count:"<<rowcount<<endl; 
                unsigned int fieldcount=mysql_num_fields(result); 
                MYSQL_FIELD *field=NULL; 
                for(unsigned int i=0;i<fieldcount;i++) 
                
       
                    field=mysql_fetch_field_direct(result,i); 
                    cout<<field->name<<" "
                
                cout<<endl; 
                MYSQL_ROW row=NULL; 
                row=mysql_fetch_row(result); 
                while(NULL!=row) 
                
       
                    for(int i=0;i<fieldcount;i++){ 
       
                        cout<<row[i]<<" "
       
                    
                    cout<<endl; 
                    row=mysql_fetch_row(result); 
       
                
        }else
       
                cout<<"mysql_query select data failed"<<endl; 
                mysql_close(&mysql); 
                return -1; 
        
        pause(); 
        /*sql="drop table user_info"; 
        if(0==mysql_query(&mysql,sql.c_str())) 
        
       
                cout<<"mysql_query drop table succeed"<<endl; 
        }else{ 
                cout<<"mysql_query drop table failed"<<endl; 
                mysql_close(&mysql); 
                return -1; 
       
        }  */
        mysql_free_result(result); 
        mysql_close(&mysql); 
        mysql_server_end(); 
       
       
        system("pause"); 
        return 0; 
    }

      运行结果:

     至此连接成功。。哈哈。。

    补充:c/c++连接数据库的方式不止一种,上面介绍的是通过MySQL自身提供C语言形式的API来进行数据库连接。除此之外还可以用connector c++来进行连接。不同的连接方式在效率、稳定性方面存在差异。

    参考原文传送门:https://www.cnblogs.com/jycboy/p/5170637.html

  • 相关阅读:
    如何启用EMGrid/Cloud Control的HTTP而非HTTPS协议登陆
    Oracle Enterprise Manager 12c 新特性:实时RealTime Addm
    php 面向对象三大特征
    C++ 运算符重载
    C++面向对象_复制构造函数+构造函数+析构函数+static+友元
    位运算符和位运算
    SqlHelper.class.php
    C++ 流
    C++ 虚函数与多态
    C++ 继承与派生
  • 原文地址:https://www.cnblogs.com/mlgjb/p/8798361.html
Copyright © 2011-2022 走看看