MFC连接postgre数据库
配置环境:
include:libpq-fe.h文件
lib:libpq.lib文件
dll:libeay32.dll,libiconv-2.dll,libintl-8.dll,libpq.dll,ssleay32.dll五个文件
连接代码:
PGresult *res;//存放结果
PGconn *m_conn;
//连接数据库
m_conn = PQsetdbLogin(ip,port,"","",dbname,user,password);
PQsetClientEncoding(m_conn,"GBK");
if (PQstatus(m_conn) != CONNECTION_OK)
{
//连接数据库失败,就输出失败原因
CString strErr;
strErr.Format(_T("连接数据库失败 [%s]"),CString(PQerrorMessage(m_conn)));
AfxMessageBox(strErr);
PQfinish(m_conn);
return;
}
//定义数据库查询语句得到查询结果
string strsql = "select * from tb_way_performance where sp_way_id>0" ;
res = PQexec(m_conn, strsql.c_str());
//根据字段值获得字段way_id所在列
int col_way_id = PQfnumber(res,"way_id");
//获取列值col_way_id 和行值i得到单元格的值
CString str_way_id(PQgetvalue(result,i,col_way_id));
最后附带libpq连接postgre数据库的api文档
http://www.kuqin.com/postgreSQL8.1_doc/libpq.html