MFC中如何用C API连接MYSQL在控制台中连接成功的代码如下
[code]
#include "stdafx.h"#include <windows.h>
#include <iostream>
#include <mysql.h>
#include <stdio.h>
using namespace std;
int main()
{
 //connection params
 FILE *file;
 char *host = "localhost";
 char *user = "root";
 char *pass = "123456";
 char *db = "mysql";
 //sock
 MYSQL *sock;
 MYSQL_RES *results;
 MYSQL_ROW record;
 
 sock = mysql_init(0);
 if (sock) cout << "sock handle ok!" << endl;
 else {
 cout << "sock handle failed!" << mysql_error(sock) << endl;
 }
 //connection
 if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
 cout << "connection ok!" << endl;
 else {
 cout << "connection fail: " << mysql_error(sock) << endl;
 }
 mysql_set_character_set(sock,"gb2312");
 //connection character set
 cout << "connection character set: " << mysql_character_set_name(sock) << endl;
 //wait for posibility to check system/mysql sockets
 
 if(mysql_query(sock,"select * from admin_user"))
 {
 cout<<"very good!"<<endl;
 }
 results=mysql_store_result(sock);
 printf("用户名\t等级\n");
 while(record=mysql_fetch_row(results))
 {
 printf("%s\t%s \n",record[1],record[3]);
 
 }
 mysql_free_result(results);
 system("PAUSE");
 
 //closing connection
 mysql_close(sock);
 return EXIT_SUCCESS;
}
[/code]
具体的编译连接环境如下: 我的mysql安装路径为d:\mysql
所以要在VC中设置include路径和lib的路径。添加MySql的include目录到VC工作台中
Project->Settings->C/C++->Category->Preprocessor->Additional include directories中添加:D:\mysql5\include。
添加lib的路径:Tools->Options->Directories中选择Library files,然后添加lib的目录:D:\MYSQL\LIB
然后在Project->Settings->Link中,添加libmysql.lib到Object/libray modules中。还要确保Project Options中为/subsystem:console而不是/subsystem:window,否则编译错误
在MFC中该如何使用这些代码呢?
分不多的话可以再加

解决方案 »

  1.   


    下面是代码
    #include "stdafx.h"#include <windows.h>
    #include <iostream>
    #include <mysql.h>
    #include <stdio.h>
    using namespace std;
    int main()
    {
     //connection params
     FILE *file;
     char *host = "localhost";
     char *user = "root";
     char *pass = "123456";
     char *db = "movee";
     //sock
     MYSQL *sock;
     MYSQL_RES *results;
     MYSQL_ROW record;
     
     sock = mysql_init(0);
     if (sock) cout << "sock handle ok!" << endl;
     else {
     cout << "sock handle failed!" << mysql_error(sock) << endl;
     }
     //connection
     if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
     cout << "connection ok!" << endl;
     else {
     cout << "connection fail: " << mysql_error(sock) << endl;
     }
     mysql_set_character_set(sock,"gb2312");
     //connection character set
     cout << "connection character set: " << mysql_character_set_name(sock) << endl;
     //wait for posibility to check system/mysql sockets
     
     if(mysql_query(sock,"select * from admin_user"))
     {
     cout<<"very good!"<<endl;
     }
     results=mysql_store_result(sock);
     printf("用户名\t等级\n");
     while(record=mysql_fetch_row(results))
     {
     printf("%s\t%s \n",record[1],record[3]);
     
     }
     mysql_free_result(results);
     system("PAUSE");
     
     //closing connection
     mysql_close(sock);
     return EXIT_SUCCESS;
    }
      

  2.   

    还要确保Project   Options中为/subsystem:console而不是/subsystem:window,否则编译错误
    这个在MFC中去掉。
    我用过,只需要设置头文件路径,lib路径,包含头文件和lib就可以用了。
      

  3.   

    我用过没有用呢
    是不是直接把 /subsystem:window删除,谢谢。请教下详细的方法。
    是用类的还是如上直接建一个新的mysql.cpp文件直接使用
    还是在oninitDialog的下面直接使用,谢谢!
      

  4.   

    直接
    #include "mysql.h"
    #pragama comment(lib,"libmysql") 最多也再#include <winsock2.h> #pragma comment(lib,"ws2_32.lib")然后一样的用啊
      

  5.   

    e\mysql_com.h(183) : error C2146: syntax error : missing ';' before identifier 'fd'
    d:\program files\microsoft visual studio\vc98\include\mysql_com.h(183) : error C2501: 'SOCKET' : missing storage-class or type specifiers
    d:\program files\microsoft visual studio\vc98\include\mysql_com.h(183) : error C2501: 'fd' : missing storage-class or type specifiers
    d:\program files\microsoft visual studio\vc98\include\mysql_com.h(358) : error C2065: 'SOCKET' : undeclared identifier
    d:\program files\microsoft visual studio\vc98\include\mysql_com.h(358) : error C2146: syntax error : missing ')' before identifier 's'
    d:\program files\microsoft visual studio\vc98\include\mysql_com.h(359) : error C2059: syntax error : ')'\submithelpDlg.cpp(180) : error C2059: syntax error : '<'
    \submithelpDlg.cpp(182) : error C2059: syntax error : '<'
    \submithelpDlg.cpp(186) : error C2059: syntax error : '<'
    \submithelpDlg.cpp(192) : error C2059: syntax error : '<'
    submithelpDlg.cpp(197) : error C2059: syntax error : '<'
    Error executing cl.exe.调试出错好像是链接库出错,但在控制台是这样设置的这个不论更改与否,都出同样的错。
    Options中为/subsystem:console而不是/subsystem:window,否则编译错误 
      

  6.   

    以前记得好像哪篇文章说过要在
    #include"mysql.h"
    之前加入
    #include"winsock.h"(记不清是不是这个头文件了,有点相似)
      

  7.   

    mysql是需要sock支持的,所以你得加上winsock.h
      

  8.   

    是这个头文件#include "winsock.h"
    加在#include"mysql.h"之前,只不过出错更多。
      

  9.   

    原来是多加了个#include "mysql.h"正确的应该是加入头文件#include <iostream.h>
    #include "winsock.h"
    #include "mysql.h"
    #include   <stdio.h>上面的
    ubmithelpDlg.cpp(180)   :   error   C2059:   syntax   error   :   ' <'
    \submithelpDlg.cpp(182)   :   error   C2059:   syntax   error   :   ' <'
    \submithelpDlg.cpp(186)   :   error   C2059:   syntax   error   :   ' <'
    \submithelpDlg.cpp(192)   :   error   C2059:   syntax   error   :   ' <'
    submithelpDlg.cpp(197)   :   error   C2059:   syntax   error   :   ' <'
    Error   executing   cl.exe. 
    这些错误是因为是复制粘贴的,所以cout  与“< <”之间分隔太大
    或有全角字符在内,重新编辑下就可以了现在是LINK出错。。即subsystem:window出错。和一个File警告。warning C4101: 'file' : unreferenced local variable
    Linking...
    LINK : fatal error LNK1117: syntax error in option "subsystem:window"
    Error executing link.exe.help.exe - 1 error(s), 2 warning(s)
      

  10.   

    用/subsystem:console 时LINK : LNK6004: Debug/help.exe not found or not built by the last incremental link; performing full link
    libcmtd.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/submithelp.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.help.exe - 2 error(s), 0 warning(s)又出错如下
      

  11.   

    应该是用subsystem:window只不过好像这里要更改下语法。
      

  12.   

    把subsystem:window/incremental:yes 
    改为subsystem:window即删除/incremental:yes 调试成功,但运行时,提示内存读取错误,真麻烦呢
      

  13.   

    运行时"0x10008d86“指令引用的
    "0x0000000c"内存,该内存不能为“
    "read"要终止程序,请单击“确定”
    要调试程序,请单击“取消”检查内存分配是否成功,
    int* pInt = new[500];
    if( !pInt ) 
    { AfxmessageBox("内存分配不成功");}
      

  14.   

    调试时提示出错在这一行
    while(record=mysql_fetch_row(results))把整个循环注释掉可以运行。
      

  15.   

    改为if语句之后,可以
    再改回来while语句也可以
    真奇怪
      

  16.   

    好像没有这么麻烦吧,
    设置mysql的路径后,把dll和lib拷到工程下
    在需要的地方添加
    #include <winsock2.h>
    #include <mysql.h>再在Alt+F7中Library   files里添加
    ws2_32.lib libmysql.lib
    即可至于工程类型是windows还是console都可以的
      

  17.   

    #include <iostream>
    #include <mysql.h>
    #include <winsock.h>
    #pragma comment( lib, "libmysql.lib");
    //此句话和在附加依赖项中增加libmysql.lib 的功能一样
    usingnamespace std;
    int main(char **args)
    {
            MYSQL mysql;
            mysql_init(&mysql);
            if (mysql_real_connect(&mysql,"localhost","root","","test",3306,0,0))
            {
                    cout<<"ok"<<endl;
                    return 0;
            }
            else
            {
                    int i = mysql_errno(&mysql);
                    constchar * s = mysql_error(&mysql);
                    cout << s<<endl;
            }
    }
    相关设置:
    1、附加包含头文件的目录,include就是mysql-5.0.27-win32\Include文件夹。
    2、附加库目录,MySql lib中的文件就是mysql-5.0.27-win32\lib\opt中的文件
    3、附加依赖项,名称为libmysql.lib
    4、将libmysql.dll拷贝到debug文件夹中,libmysql.dll在lib文件夹中有 
      

  18.   


    作个总结,备忘
    #include  <windows.h> 
    #include  <iostream> 
    #include  <mysql.h> 
    #include  <stdio.h> 
    using namespace std;
    int main() 

    //connection params 
     FILE *file; 
     char *host = "localhost"; 
     char *user = "root"; 
     char *pass = "yourpassword"; 
     char *db = "mysql"; 
     //sock 
     MYSQL *sock; 
     MYSQL_RES *results; 
     MYSQL_ROW record; 
      
     sock = mysql_init(0); 
     if (sock) cout  << "sock handle ok!"  << endl; 
     else { 
     cout  << "sock handle failed!" << mysql_error(sock)<< endl; 
     } 
     //connection 
     if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0)) 
     cout  << "connection ok!"  << endl; 
     else { 
     cout << "connection fail: " << mysql_error(sock)<< endl; 
     } 
     mysql_set_character_set(sock,"gb2312"); 
     //connection character set 
     cout  << "connection character set: "  << mysql_character_set_name(sock) << endl; 
     //wait for posibility to check system/mysql sockets 
      
     if(mysql_query(sock,"select * from admin_user")) 
     { 
     cout <<"very good!" <<endl; 
     } 
     results=mysql_store_result(sock); 
     printf("User\Level\n"); 
     while(record=mysql_fetch_row(results)) 
     { 
     printf("%s\t%s \n",record[1],record[3]); 
      
     } 
     mysql_free_result(results); 
     system("PAUSE"); 
      
     //closing connection 
     mysql_close(sock); 
     return EXIT_SUCCESS;
    }
    具体的编译连接环境如下:  我的mysql安装路径为d:\mysql 
    所以要在VC中设置include路径和lib的路径。
    一、添加MySql的include目录到VC工作台中 
    Project-> Settings-> C/C++-> Category-> Preprocessor-> Additional include directories中添加:D:\mysql\include。 
    二、添加lib的路径:Tools-> Options-> Directories中选择Library files,然后添加lib的目录:D:\MYSQL\LIB 
    然后在Project-> Settings-> Link中,添加libmysql.lib到Object/libray modules中。三、还要确保Project Options中为/subsystem:console或是/subsystem:window,否则编译错误 (这一项不太重要,如果在控制台中用subsystem:console
    在MFC中用/subsystem:window
      

  19.   

    晕,运行这个程序时,提示内存无法为read,原来是因为
    这句中的
     printf("%s\t %s\n",record[1],record[3]); %s和\t 连在一起了。下面就完美了#include  <windows.h> 
    #include  <iostream> 
    #include  <mysql.h> 
    #include  <stdio.h> 
    using namespace std;
    int main() 

    //connection params 
     FILE *file; 
     char *host = "localhost"; 
     char *user = "root"; 
     char *pass = "yourpassword"; 
     char *db = "mysql"; 
     //sock 
     MYSQL *sock; 
     MYSQL_RES *results; 
     MYSQL_ROW record; 
      
     sock = mysql_init(0); 
     if (sock) cout  << "sock handle ok!"  << endl; 
     else { 
     cout  << "sock handle failed!" << mysql_error(sock)<< endl; 
     } 
     //connection 
     if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0)) 
     cout  << "connection ok!"  << endl; 
     else { 
     cout << "connection fail: " << mysql_error(sock)<< endl; 
     } 
     mysql_set_character_set(sock,"gb2312"); 
     //connection character set 
     cout  << "connection character set: "  << mysql_character_set_name(sock) << endl; 
     //wait for posibility to check system/mysql sockets 
      
     if(mysql_query(sock,"select * from admin_user")) 
     { 
     cout <<"very good!" <<endl; 
     } 
     results=mysql_store_result(sock); 
     printf("User\Level\n"); 
     while(record=mysql_fetch_row(results)) 
     { 
     printf("%s\t %s\n",record[1],record[3]); 
      
     } 
     mysql_free_result(results); 
     system("PAUSE"); 
      
     //closing connection 
     mysql_close(sock); 
     return EXIT_SUCCESS;
    }
    具体的编译连接环境如下:   我的mysql安装路径为d:\mysql  
    所以要在VC中设置include路径和lib的路径。 
    一、添加MySql的include目录到VC工作台中  
    Project->  Settings->  C/C++->  Category->  Preprocessor->  Additional include directories中添加:D:\mysql\include。  
    二、添加lib的路径:Tools->  Options->  Directories中选择Library files,然后添加lib的目录:D:\MYSQL\LIB  
    然后在Project->  Settings->  Link中,添加libmysql.lib到Object/libray modules中。 三、还要确保Project Options中为/subsystem:console或是/subsystem:window,否则编译错误 (这一项不太重要,如果在控制台中用subsystem:console 
    在MFC中用/subsystem:window