A1200266
A1200266
2016-03-04 01:49
采纳率: 75%
浏览 3.8k

VS2010下MFC 查询mysql的例子

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • devmiao
    devmiao 2016-03-04 21: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; 
    }
    
    点赞 评论
  • oyljerry
    oyljerry 2016-03-04 05:29
    点赞 评论
  • WinsenJiansbomber
    Jimbo 2016-03-04 12: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;
    }
    
    点赞 评论
  • WinsenJiansbomber
    Jimbo 2016-03-04 20:12

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

    点赞 评论
  • devmiao
    devmiao 2016-03-04 21:53
    点赞 评论
  • u013344915
    lazying_bird 2016-03-16 01:41
    点赞 评论

相关推荐