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

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查询关联的错误信息。

1 if(PQresultStatus(res) != PGRES_COMMAND_OK)
2 {
3         // throw exception or show error messages
4         cout << PQresultErrorMessage(res) << endl;
5 }
6 else
7 {
8         //do somthing...
9 }

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

 

command = "INSERT INTO mytable (username,weblog) VALUES ('ode','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,zipcode) VALUES ('SHANGHAI','200202');";
        //res = PQexec(conn,"INSERT INTO mytable (cityname,zipcode) VALUES ('SHANGHAI','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,row_number,column_number) << endl;
            }
        }
        // important !!!
        PQclear(res_getallrows);

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

posted @ 2013-05-09 11:00  ode  阅读(3446)  评论(0编辑  收藏  举报