vc6操作PostgreSQL增加错误检测和现实链接属性

老外做东西真的很细腻哈,一个新手读读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 )));
}


程序运行截图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值