最近在和postgresql打交道,今天看了一下C++连接postgresql,便做一点关于pqxx的笔记。
安装pqxx:![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
wget http://pqxx.org/download/software/libpqxx/libpqxx-4.0.tar.gz tar xvfz libpqxx-4.0.tar.gz cd libpqxx-4.0 ./configure make make install
搜索一下你postgresql安装目录里的pg_hba.conf文件 ,添加
# IPv4 local connections: host all all 127.0.0.1/32 md5
注意:
#include <pqxx/pqxx>
using namespace pqxx
这两行不能漏掉。
建立连接:
connection C("dbname = ?? user = ?? password = ?? host = ?? port = ??");
执行SQL语句:
sql = "SELECT * FROM STYLE;";
work W(C);
pqxx::result r=W.exec(sql);
其中SELECT的结果我是这样处理:
for (row = 0;row != r.size(); row++)
{
for(field = 0; field != r[row].size(); field++)
{
std::cout << r[row][field]<< " ";
}
std::cout << endl;
}
其他详情见官网http://pqxx.org/devprojects/libpqxx/doc/4.0/html/Reference/