Created 星期六 10 四月 2010
编绎
pg_config --includedir
pg_config --libdir
-I/usr/include/postgresql -lpq
加头文件libpq-fe.h
连接数据库
比如"usr=xd dbname=xun"
断开连接
void PQfinish(PGconn *conn);
获取连接的状态
char *PQdb(const PGconn *conn);//数据库
char *PQuser(const PGconn *conn);//用户
ConnStatusType PQstatus(const PGconn *conn);//状态
其中CONNECTON_OK用到了。
执行sql命令
ExecStatusType PQresultStatus(const PGresult *res); 获取返回结果的状态, PGRES_EMPTY_QUERY,PGRES_COMMAND_OK(无返回结果),PGRES_TUPLES_OK(能获取数据行,例如select等) char *PQresultErrorMessage(const PGresult *res); 获取错误信息。
用PQclear释放返回结果。
分析结果
这里返回结果的状态是 PGRES_TUPLES_OK。
int PQnfields(const PGresult *res);
列的名字
int PQfnumber(const PGresult *res,
char *PQfname(const PGresult *res, int column_number);
获取值。
char *PQgetvalue(const PGresult *res,
int column_number);
libpqxx
加头文件
类型
connection
构建它时有一个字符串参数,例如: "dbname=xun user=xun",可见postgresql文档30.1
work
它是transaction<>的重命名,创建时需要一个连接和一个独特的名字,如果是对数据库修改,需要commit(完成)或rollback(中途退出而不改变)。
执行sql命令使用work::exec,必要时考虑esc。也可通过使用cursor;
result
它包含了result::tuple,result::field,result::const_iterator,result::tuple::const_iterator
tablestream
使用COPY命令进行读写
异常
broken_connection : 不能连接
sql_error : 语句错误
in_doubt_error
函数
esc : 在字符串中所有的符号当做字符来看待。
to_string 和 from_string
在看参考时注意module页面。
编绎
类型转化
在使用field::to之前需特异化模板函数from_string;
result,tuple的访问都像标准容器都一样,可以通过at,[],还有const_iterator;
tuple可以通过一个数,或列名的字符串获得一个field.
使用transactor
从transactor类派生,自定义operator()函数。然后以transacotr为参数调用connection::perform;