老外做东西真的很细腻哈,一个新手读读document 竟然如此的顺利
看来比想象的多多了。
如果远程访问的化,需要配置 conf文件,以后在看去吧,暂时先用127.0.0.1 代替
上代码
// pgDemo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "libpq-fe.h"
static void show_connection_attributes( const PGconn * conn );
static const char * check( const char * value );
int main(int argc, char* argv[])
{
PGconn * connection;
/** 顺带解释下,链接字符串的构成
Table 8.2. Connection Attributes Connect-String Keyword
Environment
Variable Example
user
PGUSER user=korry
password
PGPASSWORD password=cows
dbname
PGDATABASE dbname=accounting
host
PGHOST host=jersey
hostaddr
PGHOSTADDR hostaddr=127.0.0.1
service
PGSERVICE service=accounting
port
PGPORT port=5432
**/
// 用下面这个是因为不需要SSL的支持,加上IP就需要了
// 注意大小写敏感
connection = PQconnectdb( "dbname=mydb user=Eagle password=123456 hostaddr=127.0.0.1");
if ( PQstatus(connection) != CONNECTION_OK )
{
printf("%s \n", PQerrorMessage(connection) );
}
else
{
printf("connect is ok \n");
}
show_connection_attributes( connection );
PQfinish( connection);
return 0;
}
static const char * check( const char * value )
{
if( value )
return( value );
else
return( "(null)" );
}
static void show_connection_attributes( const PGconn * c )
{
printf( "dbname = %s\n", check( PQdb( c )));
printf( "user = %s\n", check( PQuser( c )));
printf( "password = %s\n", check( PQpass( c )));
printf( "host = %s\n", check( PQhost( c )));
printf( "port = %s\n", check( PQport( c )));
printf( "tty = %s\n", check( PQtty( c )));
printf( "options = %s\n", check( PQoptions( c )));
}
程序运行截图: