我在上海种太阳 2019-03-19 22:36 采纳率: 100%
浏览 741
已采纳

QT 使用子函数,进行mysql进行查询,返回一个类数组。

运行环境: QT4.8

Linux: Ubuntu 12.04.5

想完成的功能:用子函数传入参数,查询出多条记录,每条参数记录在定义好的类中,这个子函数返回这个类数组。

各位大哥,救救孩子,毕设写到这里就嗝屁了,我开始用的想法是返回一个

QString类型的数组,代码如下:

QString *selectStudentAndClass(QString holdno){
    qDebug()<<"\n\n-----into selectStudentAndClass  OK  -----";
    Student find;
    getStudent(QStoCH(holdno),find);
    QSqlQuery query;
    QString stu_no[50] = "";
    QString l_stu_no;
    int count = 0 ;

    QString dbyuju1 = QString("SELECT US_W_CL.STU_NO FROM US_W_CL WHERE US_W_CL.SWC_NUNBER = (SELECT US_W_CL.SWC_NUNBER FROM US_W_CL WHERE US_W_CL.STU_NO = \"%1\")")
            .arg(find.STU_NO);
    qDebug()<<"select US_W_CL.SWC_NUNBER sql:"<<dbyuju1;
    query.exec(dbyuju1);
    while(query.next()){
        l_stu_no = query.value(0).toString();
        if(l_stu_no.isEmpty()){
            qDebug() <<"select US_W_CL.l_stu_no ERROR " ;
           return 0;
        }
        else{
            qDebug() <<"select US_W_CL.l_stu_no is :"<<l_stu_no ;
            stu_no[count] = l_stu_no;
            qDebug() <<"***select US_W_CL.l_stu_no is :***"<<stu_no[count]<<"count is :"<<count;
           // stu_no[++count] = "\0";
            count ++;
        }
    }
    count = 0;
    while(!stu_no[count].isEmpty()){
        qDebug() <<"stu_no[count]:"<<stu_no[count]<<"count is :"<<count;
        count++;
    }
     qDebug()<<"-----into selectStudentAndClass  end  -----\n";

    return stu_no;
}

但是好像内存泄漏了,主函数中使用这个函数之后,虽然可以得到返回的值,可是调用这个值就会程序崩溃,我也崩溃了。救救孩子!!!

  • 写回答

1条回答

  • 我在上海种太阳 2019-03-20 00:59
    关注

    问题解决了。靠自己!!!!哭了,结局的思路是直接在这个贴上去的代码里面查询,然后返回的是一个student类,这样就可以了!!!!代码附上:

    Student *selectStudentAndClass(QString holdno,int &recount){
        qDebug()<<"\n\n-----into selectStudentAndClass  OK  -----";
        Student find;
        Student l_student[50];
        getStudent(QStoCH(holdno),find);
        QSqlQuery query;
        QString l_stu_no;
        int count = 0 ;
        QString dbyuju1 = QString("SELECT US_W_CL.STU_NO FROM US_W_CL WHERE US_W_CL.SWC_NUNBER = (SELECT US_W_CL.SWC_NUNBER FROM US_W_CL WHERE US_W_CL.STU_NO = \"%1\")")
                .arg(find.STU_NO);
        qDebug()<<"select US_W_CL.SWC_NUNBER sql:"<<dbyuju1;
        query.exec(dbyuju1);
        while(query.next()){
            l_stu_no = query.value(0).toString();
            if(l_stu_no.isEmpty()){
                qDebug() <<"select US_W_CL.l_stu_no ERROR " ;
               return 0;
            }
            else{
                qDebug() <<"select US_W_CL.l_stu_no is :"<<l_stu_no ;
                if(getStudentOfStuNo(l_stu_no,l_student[count])){
                    qDebug()<<"selectStudentAndClass.getStudentOfStuNo find it,\nl_student.HOLD_NO is :"<<l_student[count].HOLD_NO;
                }
                else{
                    qDebug()<<"selectStudentAndClass.getStudentOfStuNo don't find it";
                }
                count ++;
            }
        }
        recount = count;
        qDebug()<<"selectStudentAndClass.count is :"<<count<<" and "<<recount;
        qDebug()<<"\n\n-----into selectStudentAndClass  end  -----";
        return l_student;
    }
    

    本人毕设代码,各位如果要用希望更改下~谢谢,为了小白学习献身自我。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)