您的位置:首页 > 编程语言 > C语言/C++

C++中 MFC DLL如何连接mysql数据库以及按条件查询和时间的比较

2017-11-17 10:45 936 查看


1.把mysql数据库的以上文件复制到 “右键点击项目名——》在文件资源管理器中打开文件夹”中。

2.右键点击项目名——》添加——》现有项,选中从mysql复制过来的所有文件,点击添加。

3.可以写代码了。

extern "C" bool PASCAL EXPORT queryId(int numA)

{

    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    // 此处为普通函数体    

    bool flag=false;

    MYSQL m_sqlCon;  

    try{  

        mysql_init(&m_sqlCon); 

        if(!mysql_real_connect(&m_sqlCon, "IP地址","登陆名","登陆密码","数据库名",3306,NULL,0)){  

            AfxMessageBox(_T("数据库连接失败!"));

        }else{

             char select_user[255];

             CString beginDate,endDate;

             int num_col;

             MYSQL_RES *result=NULL;

             MYSQL_ROW mysql_row;

             sprintf_s(select_user, "select * from gmicus where id='%d'", numA);

             if (mysql_query(&m_sqlCon, select_user) || !(result = mysql_store_result(&m_sqlCon))) {

                 AfxMessageBox(_T("程序运行错误!"));

              }

             num_col=(int)mysql_num_fields(result);

             if(mysql_num_rows(result)==0) {

                 AfxMessageBox(_T("未找到您的信息!"));

             }else{

                 while (mysql_row = mysql_fetch_row(result))//获取具体的数据

                {

                    for (int i = 0; i < num_col; i++)

                    {       

                        if(i==2){

                            beginDate=CStringW(mysql_row[i]);

                        }

                        if(i==3){

                            endDate=CStringW(mysql_row[i]);

                        }

                    }

                }

                 CTime m_time;;

                 CTime datetime;

                 datetime=CTime::GetCurrentTime();                                                                              

                 CString str2=datetime.Format("%Y-%m-%d %H:%M:%S");  

                 /*CString str;

                 str.Format(_T("%s"), str2);

                 AfxMessageBox(str);*/

                 COleDateTime begin;

                 begin.ParseDateTime( beginDate );   

                 COleDateTime end;

                 end.ParseDateTime( endDate );

                 COleDateTime nowDate;

                 nowDate.ParseDateTime( str2 );

                 if(nowDate>=begin && nowDate<=end){

                    flag=true;

                 }else{

                    flag=false;

                 }

             }

            mysql_free_result(result);

            mysql_close(&m_sqlCon);

            getchar();

        }    

    }catch (...){  

         AfxMessageBox(_T("未知错误!"));

    }

    return flag;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql C++ mfc DLL