libpq-PostgreSQL客户端编程接口(二)----libpq中的命令执行函
libpq中的命令执行函数有:PQexec,PQexecParams,PQprepare,PQprepared。 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返回的错误信息为: 查询结果信息部分比较简单,看一段示例代码: 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 本作品由vincent zhang创作,采用知识共享署名-非商业性使用-禁止演绎 3.0 中国大陆许可协议进行许可。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |