|

楼主 |
发表于 2003-12-16 19:33:50
|
显示全部楼层
问题得到解决,代码如下;

void
on_next_clicked (GtkButton *button,
gpointer user_data)
{
int indx;
gchar *pghost,
*pgport,
*pgoptions,
*pgtty;
gchar *dbName;
int nFields;
int i,j,tmp;
gchar *drink[3]={NULL} ,*buffer;
GtkWidget *data;
data = lookup_widget (GTK_WIDGET (button), "clist2");
/* FILE *debug; */
PGconn *conn;
PGresult *res;
/*
* 开始的时候,我们设置服务器连接的参数,如果参数为空,那么系统将
* 通过查找环境变量来使用合理的缺省,如果仍然没有找到,那么就使用
* 写在程序中的常量
*/
pghost = NULL; /* 服务器服务器的主机名 */
pgport = NULL; /* 服务器服务器的端口号 */
pgoptions = NULL; /* 启动服务器服务器的特殊选项*/
pgtty = NULL; /* 未用 */
dbName = "root";
/* 和数据库建立链接 */
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
/*
* 检查一下与服务器的连接是否成功建立
*/
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
/* debug = fopen("/tmp/trace.out","w"); */
/* PQtrace(conn, debug); */
/* 开始一个事务块 */
res = PQexec(conn, "BEGIN");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "BEGIN command failed\n");
PQclear(res);
exit_nicely(conn);
}
/*
* 如果不再需要 PGresult 了,我们应该 PQclear,以避免内存泄漏
*/
PQclear(res);
/*
* 从存储数据库信息的系统表 pg_database 中抓取数据行
*/
res = PQexec(conn, "DECLARE mycursor CURSOR FOR SELECT * FROM dynamic_host_table");
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "DECLARE CURSOR command failed\n");
PQclear(res);
exit_nicely(conn);
}
PQclear(res);
res = PQexec(conn, "FETCH ALL in mycursor");
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "FETCH ALL command didn't return tuples properly\n");
PQclear(res);
exit_nicely(conn);
}
/* 首先,打印属性名 */
nFields = PQnfields(res);
for (i = 0; i < nFields; i++)
g_print("%-15s", PQfname(res, i));
g_print("\n\n");
/* 然后,打印数据行 */
for (i = 0; i < PQntuples(res); i++)
{
tmp=gtk_clist_append( (GtkCList *) data, drink);
for (j = 0; j < nFields; j++)
{
// buffer=(gchar*)(malloc(80));
// strcpy(buffer,PQgetvalue(res,tmp,j));
gtk_clist_set_text(data,tmp,j,(const gchar*)(PQgetvalue(res,i,j)));
//g_print("%s ",buffer);
}
//g_print("\n");
// printf("%-15s", PQgetvalue(res, i, j));
}
//g_print("testing................\n");
PQclear(res);
/* 关闭游标 */
res = PQexec(conn, "CLOSE mycursor");
PQclear(res);
//g_print("testing................\n");
/* 提交事务 */
res = PQexec(conn, "COMMIT");
PQclear(res);
/* 关闭与数据库的连接并且清理 */
PQfinish(conn);
// exit_nicely(conn);
return;
} |
|