文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>PostgreSQL C Program

PostgreSQL C Program

时间:2010-01-23  来源:admin126com

 

#include <stdio.h>
#include "libpq-fe.h"

void
exit_nicely(PGconn * conn)
{
    PQfinish(conn);
    exit(1);
}

int main()
{
    char * pghost, * pgport, * pgoptions, * pgtty;
    char * dbName;

    char name[256];

    int i, num_records;

    PGconn * conn;
    PGresult * res;

    pghost = NULL;
    pgport = NULL;
    pgoptions = NULL;
    pgtty = NULL;

    dbName = "test";

    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);
    }

    res = PQexec(conn, "SET DateStyle = 'European'");

    if ((!res) || (PQresultStatus(res) != PGRES_COMMAND_OK))
    {
        fprintf(stderr, "SET command failed\n");
        PQclear(res);
        exit_nicely(conn);
    }
    PQclear(res);

    res = PQexec(conn,
        "SELECT first_name, last_name, hired_at"
        " FROM employees"
        " ORDER BY last_name, first_name"
    );
    if ((!res) || (PQresultStatus(res) != PGRES_TUPLES_OK))
    {
        fprintf(stderr, "SELECT command did not return tuples properly\n");
        PQclear(res);
        exit_nicely(conn);
    }

    printf("%-40s%-20s\n", "Name:", "Hired At:");
    for(i=0;i<60;i++)
    {
        printf("-");
    }
    printf("\n");

    num_records = PQntuples(res);

    for(i = 0 ; i < num_records ; i++)
    {
        sprintf(name, "%s %s", PQgetvalue(res, i, 0), PQgetvalue(res, i, 1));
        printf("%-40s%-20s\n", name, PQgetvalue(res, i, 2));
    }

    PQclear(res);

    PQfinish(conn);

    return 0;

}

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载