加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

libpq-PostgreSQL客户端编程接口(二)----libpq中的命令执行函

发布时间:2020-12-13 17:38:48 所属栏目:百科 来源:网络整理
导读:libpq中的命令执行函数有:PQexec,PQexecParams,PQprepare,PQprepared。 PQexec给服务器提交一条命令并且等待结果 定义:PGresult *PQexec(PGconn *conn,const char *command); PQexec返回一个PGresult指针或者NULL。 PGresult *res;const char *command = "

libpq中的命令执行函数有:PQexec,PQexecParams,PQprepare,PQprepared。
PQexec给服务器提交一条命令并且等待结果
定义:PGresult *PQexec(PGconn *conn,const char *command);
PQexec返回一个PGresult指针或者NULL。

PGresult *res;
const char *command = "INSERT INTO mytable (username,weblog) VALUES ('ode','http://ode.cnblogs.com');";
res = PQexec(conn,command);

命令函数的执行结果可以由PQresultStatus来判断, PQresultErrorMessage用来捕获与PQexec查询关联的错误信息。

if(PQresultStatus(res) != PGRES_COMMAND_OK)
{
    // throw exception or show error messages
       cout << PQresultErrorMessage(res) << endl;
}
else
{
    //do somthing...
}

也可以在命令行字符串command中包含多个执行语句,每个语句用分号隔开。PQexec中调用的多个查询,默认是在一个事务中执行的,例如下面这个例子,假设我的command语句有笔误,那么多条语句均不会执行:

command = "INSERT INTO mytable (username,'http://ode.cnblogs.com');INSERT INTO mytable (username1,weblog) VALUES ('odevincent','http://odevincent.blog.51cto.com');";

第二条语句是错误的,第一条语句也不会执行。PQresultmessage返回的错误信息为:
错误: 关系 "mytable" 的 "username1" 字段不存在
在PostgreSQL的文档中介绍:“除非在查询字串里有明确的 BEGIN/COMMIT 命令用于把整个字串分隔成多个事务。请注意这样返回的 PGresult 结构只描述字串里执行的最后一条命令的结果。”这里没有进行实验了,实际开发中如果多个事务,建议定义不同的PQexec来执行。如果是比较复杂的业务,可以考虑使用存储过程或其他方式。

查询结果信息部分比较简单,看一段示例代码:

PGresult *res;
        const char* strSQL = "INSERT INTO mytable (cityname,zipcode) VALUES ('SHANGHAI','200200');INSERT INTO mytable (cityname1,'200202');";
        //res = PQexec(conn,"INSERT INTO mytable (cityname,'200200');");
        res = PQexec(conn,strSQL);
        if(PQresultStatus(res) != PGRES_COMMAND_OK)
        {
            cout << "command faild! PQresultStatus=" << PQresultStatus(res) << endl;
            // print error message
            cout << PQresultErrorMessage(res) << endl;
        }
        else
        {
            cout << "commit success.OID is : " << PQoidValue(res) << endl;
        }
        // important !!!
        PQclear(res);
        PGresult *res_getallrows;
        res_getallrows = PQexec(conn,"SELECT cityname,zipcode FROM mytable;");
        if(PQresultStatus(res_getallrows) != PGRES_TUPLES_OK)
        {
            cout << "QUERY faild! PQresultStatus=" << PQresultStatus(res_getallrows) << endl;
            // print error message
            cout << PQresultErrorMessage(res_getallrows) << endl;
        }
        else
        {
            // rows count
            cout << "PQntuples : " << PQntuples(res_getallrows) << endl;
            // fields count
            cout << "PQnfields : " << PQnfields(res_getallrows) << endl;
            int fieldsCount = PQnfields(res_getallrows) ;
            for(int fieldIndex = 0;fieldIndex < fieldsCount;++fieldIndex)
            {
                cout << "field " << fieldIndex << " name is : " << PQfname(res_getallrows,fieldIndex) << endl;
                // PQfformat
                cout << "field " << fieldIndex << " format is : " << PQfformat(res_getallrows,fieldIndex) << endl;
                // PQfmod
                cout << "field " << fieldIndex << " mod is : " << PQfmod(res_getallrows,fieldIndex) << endl;
                // PQfsize
                cout << "field " << fieldIndex << " size is (varchar will return -1.): " << PQfsize(res_getallrows,fieldIndex) << endl;
            }
            cout << "cityname fnumber : " << PQfnumber(res_getallrows,"cityname") << endl;
            cout << "zipcode fnumber : "  << PQfnumber(res_getallrows,"zipcode") << endl;
            /*
             *char *PQgetvalue(const PGresult *res,int row_number,int column_number);
            */
            int row_number = 0,column_number = 0;
            if(!PQgetisnull(res_getallrows,row_number,column_number) )
            {
                cout << "row" << row_number << ",column" << column_number << " value is : " << PQgetvalue(res_getallrows,column_number) << endl;
            }
        }
        // important !!!
        PQclear(res_getallrows);

到这里,已经可以用libpq完成大部分的EnterpriseDB(PostgreSQL Plus Advanced Server)数据库日常开发工作了。



作者:vincent zhang
出处:http://ode.cnblogs.com http://odevincent.blog.51cto.com
Email:wsaspx#hotmail.com

知识共享许可协议


本作品由vincent zhang创作,采用知识共享署名-非商业性使用-禁止演绎 3.0 中国大陆许可协议进行许可。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读