LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 858|回复: 4

大侠们来帮我看看这段程序有什么问题啊~~

[复制链接]
发表于 2003-12-16 14:48:41 | 显示全部楼层 |阅读模式
void
on_next_clicked                        (GtkButton       *button,
                                        gpointer         user_data)
{
    int indx;
    gchar       *pghost,
               *pgport,
               *pgoptions,
               *pgtty;
    gchar       *dbName;
    int         nFields;
    int         i,
                j;
        gchar *drink[4][3] ,*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++)
    {
        for (j = 0; j < nFields; j++)
        {
                buffer=(gchar*)(malloc(2+strlen(PQgetvalue(res,i,j))));
                strcpy(buffer,PQgetvalue(res,i,j));
                drink[j]=buffer;
g_print("%s\n",buffer);
        }
//            printf("%-15s", PQgetvalue(res, i, j));
        gtk_clist_append( (GtkCList *) data, drink);

    }
//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;
}
刚开始学GNU编程,想用它操纵数据库,可每次执行到这总会出现内存溢出错误,不知道是什么原因,望高手指教
发表于 2003-12-16 16:45:15 | 显示全部楼层
用gdb跟踪一下。
 楼主| 发表于 2003-12-16 17:34:29 | 显示全部楼层
跟踪过了,可是不大熟悉gdb,所以没找到原因是什么
 楼主| 发表于 2003-12-16 17:49:30 | 显示全部楼层
我把 向list 添加行那一段注释掉,就没问题了,看来是那儿的问题,可是 该怎么添加呢?
 楼主| 发表于 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;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表