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;
}

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

问题解决了。靠自己!!!!哭了,结局的思路是直接在这个贴上去的代码里面查询,然后返回的是一个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;
}

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