| ength
GetLine(char* string, int length)
同 PQgetline
PutLine(const char* string)
同 PQputline
EndCopy()
同 PQendcopy
大对象操作使用 PgLargeObject 类来操作。
例子程序
以 postgres 用户或者具有建立数据库用户权限的用户登录 Linux,先建立数据库。
[zuojin@itpark85 zuojin]$createdb test_db
然后使用 psql 连接到数据库
[zuojin@itpark85 zuojin]$psql test_db
连接到数据库建立表,如下图所示
插入测试用数据,如图:
(注意:psql中输入SQL语句要以"; "结束才执行,帮助命令是"\\?",退出命令是"\\q")。
上一页 [1] [2] [3] [4] 下一页
1)C语言例子
#include <stdio.h>
#include <libpq-fe.h>
int main() {
PGconn *conn;
PGresult *res;
char *pghost = NULL;
char *pgport =NULL;
char *pgoptions =NULL;
char *pgtty = NULL;
char *dbname ="test_db";
/**数据库名*/
int i = 0,t = 0,s,k;
conn = PQsetdb(pghost,pgport,pgoptions,pgtty,dbname);
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr,"Connection to database '%s' failed!\\n",dbname);
PQfinish(conn);
eturn 0;
}
res = PQexec(conn,"SELECT * FROM test");
/**运行查询命令*/
if( PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr,"Exec Query Fauled!\\n");
PQclear(res);
return 0;
}
i = PQntuples(res);
/**取得查询的结果的记录的数量*/
t = PQnfields(res);
/**取得字段数量*/
for(s=0; s<i;s++) {
for (k = 0; k<t; k++) {
printf("%s",PQgetvalue(res,s,k));
printf(" ");
}
printf("\\n");
}
PQfinish(conn);
PQclear(res);
return 0;
}
运行结果如图所示
2)C++例子
#include <iostream.h>
#include <libpq++.h>
int main() {
char query_string[256]= "SELECT * FROM test;";
PgDatabase data("dbname = test_db");
if (data.ConnectionBad()) {
cout <<"connected failed" << endl;
cout <<"Error is "<<data.ErrorMessage() << endl;
exit(1);
}
if (! data.ExecTuplesOk(query_string)) {
cout<<"Query Failed!" << endl;
exit(1);
}
for(int k=0; k<data.Fields(); k++) /**显示字段名称*/ {
cout<<data.FieldName(k);
cout <<" " ;
}
cout<<endl;
for (int i = 0; i < data.Tuples(); i++) /**取得查询结果的记录数量*/ {
for(int k=0; k<data.Fields(); k++) {
cout << data.GetValue(i,k);
cout <<" | " ;
}
cout<<endl;
}
return 0 ;
}
运行结果如图所示
上一页   上一页 [1] [2] [3] [4] 下一页
|