现在的位置: 首页 > 综合 > 正文

QT SQL 语句相关的返回处理

2012年08月01日 ⁄ 综合 ⁄ 共 929字 ⁄ 字号 评论关闭

1. 当获取最大值时,如果表中无记录的情况下,返回是 QString 类型的 0;

    QString sql = "select Max(id) from test";
    if(dbs.Exec(sql))
    {
        if(dbs.hasNextItem())
        {           
            qDebug()<<dbs.getCulColumnValue(0); //输出 "0"
        }
    }

2.当获取某个字段值时,如果能搜索到对应的记录,但是该字段为空,则返回NULL

    QString sql = "select name from test where id=1";
    if(dbs.Exec(sql))
    {
        if(dbs.hasNextItem())
        {           
            if(dbs.getCulColumnValue("name") == NULL)
            {
                qDebug()<<"NULL";  //输出 NULL
            }
            else
            {
                qDebug()<<dbs.getCulColumnValue("name");
            }
        }
        else
        {
            qDebug()<<"ELSE"<<dbs.getCulColumnValue("name");
        }
    }

3.其他情况:

QString
sql=
"select *
fromtest";

    if(dbs.Exec(sql))
    {
        if(dbs.hasNextItem())// 有记录
        {
            if(dbs.getCulColumnValue("name") == NULL) //字段为空
            {
                qDebug()<<"name is null";
            }
            else
            {
                qDebug()<<"name is null";
            }
            qDebug()<<"D";
        }
        else //木有搜索到记录
        {
            qDebug()<<"Mu ";
        }

    }
    else //表不存在或其他原因导致SQL 语句执行失败
    {

    }
 
 

抱歉!评论已关闭.