我在上海种太阳 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;
    }
    

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

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

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面