2 a1200266 A1200266 于 2016.03.04 09:49 提问

VS2010下MFC 查询mysql的例子

请问谁能指导VS2010下MFC 查询mysql的例子?局域网http://192.168.1.223上通过 phpstudy建立一个mysql数据库,用户名root,密码note2016,端口3306,数据库名cxdayang,表名user,请问如何编写一个MFC的登录小程序(做为例子)。我百度了三天,按教程操作还是出错,由于C++仅学完基础语法,MFC仅入门,请有能力的帮忙做出这样的一个小例子供我参详好吗?本人WIN7 64位,VS2010

8个回答

devmiao
devmiao   Ds   Rxr 2016.03.05 05:52
已采纳
 #include<Windows.h> 
#include<string> 
#include<iostream> 
#include<mysql.h> 
#include<stdio.h>
int main()
{
    //connectionparams 
    char *host = "192.168.1.223"; 
    char *user = "root"; 
    char *pass = "note2016"; 
    char *db = "cxdayang"; 
    unsignedint port = 3306; //server port 
    MYSQL *sock; 
    MYSQL_RES *res; 
    MYSQL_FIELD * fd; 
    MYSQL_ROW row; 
    char szSqlText[500]="";
    inti; 
    inttcreate=0;
    sock=mysql_init(0); 
    if(sock &&mysql_real_connect(sock,host,user,pass,db,0,NULL,0)) 
    { 
        std::cout<<"connect mysqlsucceed!"<<std::endl; 
       //mysql_set_character_set(sock,"utf8"); 
        mysql_query(sock, "set namesgb2312"); 
        //mysql_query(sock, "SET NAMESGBK"); //设置编码格式,否则在cmd下无法显示中文 
       //connectioncharacter set 
       //waitfor possibility to checksystem/mysqlmysqlets 
       if(tcreate) 
       { 
           sprintf(szSqlText,"create table datatable (id int(3) auto_increment not null primarykey,timedatetime,name char(10) not null,addressvarchar(20),year date,s1char(11) ,s2 int default '1') CHARACTER SET utf8");//构造SQL语句 新建一张表 
          std::cout<<"Create table tttable!"<<std::endl; 
           tcreate = 1; 
          if(mysql_query(sock,szSqlText))//执行Dsql语句,并判断是否出错 
           { 
             std::cout<<"can't createtable:"<<mysql_error(sock)<<std::endl; 
             //tcreate = 0; 
             mysql_close(sock); 
             return FALSE; 
           } 
       } 
       sprintf(szSqlText,//向mytable这个表插入数据 注意时间格式 
           "insert intodatatable values('1','2010-10-08', 'bj','深圳一中','2010-10-08','MySQLTest',20)"); 
       std::cout<<"Insert data totable!"<<std::endl; 
       if(mysql_query( sock, szSqlText)) 
       { 
          std::cout<<"Can'tinsert data to table"<<mysql_error(sock)<<std::endl; 
           mysql_close( sock); 
           return FALSE ; 
       }
      sprintf(szSqlText, "select* from datatable"); 
       std::cout<<"select* fromdatatable!"<<std::endl; 
       if(mysql_query( sock, szSqlText))//进行数据检索并检查有无错误 
       { 
           mysql_close( sock); 
           return FALSE ; 
       } 
       else 
       { 
          std::cout<<"mysql_store_result!"<<std::endl; 
           res=mysql_store_result( sock) ;//取得查询结果,保存查询到的数据到res 
           i = (int)mysql_num_rows(res ) ;//取得有效记录数 
          std::cout<<"Query:"<<szSqlText<<"\n"<<i<<" recordsfound:"<<std::endl; 
           for( i = 0 ; fd =mysql_fetch_field( res ); i++ )//获取列名 
             std::cout<<fd->name<<"\t"; 
          std::cout<<std::endl;//输出各字段名 
           while(row =mysql_fetch_row( res))//获取具体的数据,依次读取各条记录 
             std::cout<<row[0]<<"\t"<<row[1]<<"\t"<<row[2]<<"\t"<<row[3]<<"\t"<<row[4]<<std::endl; 
           mysql_free_result(res ); //释放结果资源 
       } 
    } 
    else//连接数据库出错 
    { 
       std::cout<<"cannot connect tothe sock sever "<<mysql_error(sock)<<std::endl; 
       mysql_close(sock); 
       return FALSE; 
    }
    mysql_close(sock);//断开连接 
    system("pause"); 
    return TRUE; 
}
devmiao
devmiao   Ds   Rxr 2016.03.05 05:52
 #include<Windows.h> 
#include<string> 
#include<iostream> 
#include<mysql.h> 
#include<stdio.h>
int main()
{
    //connectionparams 
    char *host = "192.168.1.223"; 
    char *user = "root"; 
    char *pass = "note2016"; 
    char *db = "cxdayang"; 
    unsignedint port = 3306; //server port 
    MYSQL *sock; 
    MYSQL_RES *res; 
    MYSQL_FIELD * fd; 
    MYSQL_ROW row; 
    char szSqlText[500]="";
    inti; 
    inttcreate=0;
    sock=mysql_init(0); 
    if(sock &&mysql_real_connect(sock,host,user,pass,db,0,NULL,0)) 
    { 
        std::cout<<"connect mysqlsucceed!"<<std::endl; 
       //mysql_set_character_set(sock,"utf8"); 
        mysql_query(sock, "set namesgb2312"); 
        //mysql_query(sock, "SET NAMESGBK"); //设置编码格式,否则在cmd下无法显示中文 
       //connectioncharacter set 
       //waitfor possibility to checksystem/mysqlmysqlets 
       if(tcreate) 
       { 
           sprintf(szSqlText,"create table datatable (id int(3) auto_increment not null primarykey,timedatetime,name char(10) not null,addressvarchar(20),year date,s1char(11) ,s2 int default '1') CHARACTER SET utf8");//构造SQL语句 新建一张表 
          std::cout<<"Create table tttable!"<<std::endl; 
           tcreate = 1; 
          if(mysql_query(sock,szSqlText))//执行Dsql语句,并判断是否出错 
           { 
             std::cout<<"can't createtable:"<<mysql_error(sock)<<std::endl; 
             //tcreate = 0; 
             mysql_close(sock); 
             return FALSE; 
           } 
       } 
       sprintf(szSqlText,//向mytable这个表插入数据 注意时间格式 
           "insert intodatatable values('1','2010-10-08', 'bj','深圳一中','2010-10-08','MySQLTest',20)"); 
       std::cout<<"Insert data totable!"<<std::endl; 
       if(mysql_query( sock, szSqlText)) 
       { 
          std::cout<<"Can'tinsert data to table"<<mysql_error(sock)<<std::endl; 
           mysql_close( sock); 
           return FALSE ; 
       }
      sprintf(szSqlText, "select* from datatable"); 
       std::cout<<"select* fromdatatable!"<<std::endl; 
       if(mysql_query( sock, szSqlText))//进行数据检索并检查有无错误 
       { 
           mysql_close( sock); 
           return FALSE ; 
       } 
       else 
       { 
          std::cout<<"mysql_store_result!"<<std::endl; 
           res=mysql_store_result( sock) ;//取得查询结果,保存查询到的数据到res 
           i = (int)mysql_num_rows(res ) ;//取得有效记录数 
          std::cout<<"Query:"<<szSqlText<<"\n"<<i<<" recordsfound:"<<std::endl; 
           for( i = 0 ; fd =mysql_fetch_field( res ); i++ )//获取列名 
             std::cout<<fd->name<<"\t"; 
          std::cout<<std::endl;//输出各字段名 
           while(row =mysql_fetch_row( res))//获取具体的数据,依次读取各条记录 
             std::cout<<row[0]<<"\t"<<row[1]<<"\t"<<row[2]<<"\t"<<row[3]<<"\t"<<row[4]<<std::endl; 
           mysql_free_result(res ); //释放结果资源 
       } 
    } 
    else//连接数据库出错 
    { 
       std::cout<<"cannot connect tothe sock sever "<<mysql_error(sock)<<std::endl; 
       mysql_close(sock); 
       return FALSE; 
    }
    mysql_close(sock);//断开连接 
    system("pause"); 
    return TRUE; 
}
devmiao
devmiao   Ds   Rxr 2016.03.05 05:52
 #include<Windows.h> 
#include<string> 
#include<iostream> 
#include<mysql.h> 
#include<stdio.h>
int main()
{
    //connectionparams 
    char *host = "192.168.1.223"; 
    char *user = "root"; 
    char *pass = "note2016"; 
    char *db = "cxdayang"; 
    unsignedint port = 3306; //server port 
    MYSQL *sock; 
    MYSQL_RES *res; 
    MYSQL_FIELD * fd; 
    MYSQL_ROW row; 
    char szSqlText[500]="";
    inti; 
    inttcreate=0;
    sock=mysql_init(0); 
    if(sock &&mysql_real_connect(sock,host,user,pass,db,0,NULL,0)) 
    { 
        std::cout<<"connect mysqlsucceed!"<<std::endl; 
       //mysql_set_character_set(sock,"utf8"); 
        mysql_query(sock, "set namesgb2312"); 
        //mysql_query(sock, "SET NAMESGBK"); //设置编码格式,否则在cmd下无法显示中文 
       //connectioncharacter set 
       //waitfor possibility to checksystem/mysqlmysqlets 
       if(tcreate) 
       { 
           sprintf(szSqlText,"create table datatable (id int(3) auto_increment not null primarykey,timedatetime,name char(10) not null,addressvarchar(20),year date,s1char(11) ,s2 int default '1') CHARACTER SET utf8");//构造SQL语句 新建一张表 
          std::cout<<"Create table tttable!"<<std::endl; 
           tcreate = 1; 
          if(mysql_query(sock,szSqlText))//执行Dsql语句,并判断是否出错 
           { 
             std::cout<<"can't createtable:"<<mysql_error(sock)<<std::endl; 
             //tcreate = 0; 
             mysql_close(sock); 
             return FALSE; 
           } 
       } 
       sprintf(szSqlText,//向mytable这个表插入数据 注意时间格式 
           "insert intodatatable values('1','2010-10-08', 'bj','深圳一中','2010-10-08','MySQLTest',20)"); 
       std::cout<<"Insert data totable!"<<std::endl; 
       if(mysql_query( sock, szSqlText)) 
       { 
          std::cout<<"Can'tinsert data to table"<<mysql_error(sock)<<std::endl; 
           mysql_close( sock); 
           return FALSE ; 
       }
      sprintf(szSqlText, "select* from datatable"); 
       std::cout<<"select* fromdatatable!"<<std::endl; 
       if(mysql_query( sock, szSqlText))//进行数据检索并检查有无错误 
       { 
           mysql_close( sock); 
           return FALSE ; 
       } 
       else 
       { 
          std::cout<<"mysql_store_result!"<<std::endl; 
           res=mysql_store_result( sock) ;//取得查询结果,保存查询到的数据到res 
           i = (int)mysql_num_rows(res ) ;//取得有效记录数 
          std::cout<<"Query:"<<szSqlText<<"\n"<<i<<" recordsfound:"<<std::endl; 
           for( i = 0 ; fd =mysql_fetch_field( res ); i++ )//获取列名 
             std::cout<<fd->name<<"\t"; 
          std::cout<<std::endl;//输出各字段名 
           while(row =mysql_fetch_row( res))//获取具体的数据,依次读取各条记录 
             std::cout<<row[0]<<"\t"<<row[1]<<"\t"<<row[2]<<"\t"<<row[3]<<"\t"<<row[4]<<std::endl; 
           mysql_free_result(res ); //释放结果资源 
       } 
    } 
    else//连接数据库出错 
    { 
       std::cout<<"cannot connect tothe sock sever "<<mysql_error(sock)<<std::endl; 
       mysql_close(sock); 
       return FALSE; 
    }
    mysql_close(sock);//断开连接 
    system("pause"); 
    return TRUE; 
}
devmiao
devmiao   Ds   Rxr 2016.03.05 05:53
A1200266
A1200266 谢谢,C#的已经会了,C++的现在也连上了。
接近 2 年之前 回复
u013344915
u013344915   2016.03.16 09:41
oyljerry
oyljerry   Ds   Rxr 2016.03.04 13:29
A1200266
A1200266 能改写成MFC版吗?新手不会改
接近 2 年之前 回复
A1200266
A1200266 谢谢,我再参详一下
接近 2 年之前 回复
A1200266
A1200266 谢谢,我再参详一下
接近 2 年之前 回复
WinsenJiansbomber
WinsenJiansbomber   2016.03.05 04:12

完整代码参考《MySQL开发C版》
/*

A1200266
A1200266 谢谢指导!
接近 2 年之前 回复
WinsenJiansbomber
WinsenJiansbomber   2016.03.04 20:56

搞了个C的版本,后面再搞MFC界面

 #include <stdio.h>
#include <windows.h>
#include <mysql.h> 

#define TIP_ERROR_CONNECT   "Connection error #%d!\n"
#define TIP_CONNECT_OK      "Mysql %s has connected!\n"
#define TIP_TEST            "MySQL database testing...\n"

#define SQL_TEST    "SELECT ID,post_title FROM wp_posts;\n"

#define USER        "root"
#define PASSWORD    "pass"
#define HOST        "localhost"
#define PORT        3306
#define DATABASE    "hyc_hr"

void query(MYSQL db, char *sql){

    printf(TIP_TEST);
    printf("SQL: %s", sql);

    MYSQL_RES *rs;
    MYSQL_ROW row;
    unsigned long *len;
    unsigned long fc, rc;

    if(0!=mysql_query(&db,sql)){
        printf("ERR %s\n", mysql_error(&db));
        return;
    }
    //rs = mysql_use_result(&db);
    rs = mysql_store_result(&db);
    if(NULL==rs){
        printf("ERR mysql_use_result #%d %s", mysql_errno(&db),mysql_error(&db));
        len = int(0);
    }
    fc = mysql_field_count(&db);
    rc = mysql_num_rows(rs);
    len = mysql_fetch_lengths(rs);
    if(NULL==len){
        printf("ERR mysql_fetch_lengths #%d %s", mysql_errno(&db),mysql_error(&db));
        len = new unsigned long(0);
    }
    printf("%d data rows, %d field count, %d fetch lengths.", rc, fc, *len);

    while ((row = mysql_fetch_row(rs)) != NULL)
       printf("%s\t%s\n", row[0],row[1]);
    mysql_free_result(rs); /* NEVER forget this free */

}

int main()
{

    MYSQL db;
    mysql_init(&db);
    if(mysql_real_connect(&db,HOST,USER,PASSWORD,DATABASE,PORT,NULL,0)){
        printf(TIP_CONNECT_OK, mysql_get_server_info(&db));
        query(db,SQL_TEST);
    }else{
        printf(TIP_ERROR_CONNECT, mysql_errno(&db));
        fprintf(stderr, "ERR: %s\n", mysql_error(&db));
    }

    mysql_close(&db);

    return 0;
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!