Windows Postgresql and Dev-Cpp/Dev-C++/MINGW configuration example

Поиск
Список
Период
Сортировка
От Reid Thompson
Тема Windows Postgresql and Dev-Cpp/Dev-C++/MINGW configuration example
Дата
Msg-id 4341FBB6.1000803@ateb.com
обсуждение исходный текст
Список pgsql-general
I thought it might be of interest to some to post steps to using
PostgreSQL on
Windows with the Dev-Cpp/MINGW IDE/compiler.This example is C, but the
steps for configuring the IDE/Project to use the PostgreSQL libs/headers
should be the same or very similar for C++/ecpg.

Install PostgreSQL for Windows using the windows installer(for those
who haven't used the PostgreSQL windows installer, it's a quick and
easy process).

Start Dev-C++.
Select File->New->Project.
Select Console Application.
Select C project.
Give the project a pertinent name(myPgsqlTest).
Click OK.
Create a new dir to contain the Project(give it a pertinent name).
Save the initial project.
Select Project->Project Options.
  Select the Parameters Tab.
    Click the Add Library or Object button.
    Traverse to your PostgreSQL installation 's lib directory - mine is
      ../../../PostgreSQL/8.0/lib/ and select libpq.a.
In my case, the linker window now contains
    ../../../PostgreSQL/8.0/lib/libpq.a.
  Select the Directories Tab.
    Select the Include Directories sub-tab.
    Traverse to and select your PostgreSQL installation' s include
directory -
      mine is D:\PostgreSQL\8.0\include.
      Click on Add(your Include directories window should now list this
path).
      Click on OK to close the Project Options Dialog
Insure that the db 'test' exists in your PostgreSQL instance, or change
'test'
in the conninfo to your test db when you populate the other appropriate
values
for your connection.
Replace the Dev-C++ template created main.c

#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{

    system("PAUSE");
    return 0;
}

with the attached file
(created using the libpq example from www.postgresql.org/docs)

reid
I thought it might be of interest to some to post steps to using PostgreSQL on
Windows with the Dev-Cpp / MINGW IDE / compiler.This example is C, but the
steps for configuring the IDE / Project to use the PostgreSQL libs / headers
should be the same or very similar for C++ / ecpg.

Install PostgreSQL for Windows using the windows installer(for those
who haven't used the PostgreSQL windows installer, it's a quick and
easy process).

Start Dev - C++.
Select File->New->Project.
Select Console Application.
Select C project.
Give the project a pertinent name(myPgsqlTest).
Click OK.
Create a new dir to contain the Project(give it a pertinent name).
Save the initial project.
Select Project->Project Options.
  Select the Parameters Tab.
    Click the Add Library or Object button.
    Traverse to your PostgreSQL installation 's lib directory - mine is
      ../../../PostgreSQL/8.0/lib/ and select libpq.a.
In my case, the linker window now contains
    ../../../PostgreSQL/8.0/lib/libpq.a.
  Select the Directories Tab.
    Select the Include Directories sub-tab.
    Traverse to and select your PostgreSQL installation' s include directory -
      mine is D:\PostgreSQL\8.0\include.
      Click on Add(your Include directories window should now list this path).
      Click on OK to close the Project Options Dialog
Insure that the db 'test' exists in your PostgreSQL instance, or change 'test'
in the conninfo to your test db when you populate the other appropriate values
for your connection.
Replace the Dev-C++ template created main.c

#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{

    system("PAUSE");
    return 0;
}

with (this is based on the libpq example from www.postgresql.org/docs)

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

static
void exit_nicely(PGconn * conn)
{
    PQfinish(conn);
    /***************************************************************************
    * I added this because it's the 'default' means of preventing the cmd.exe
    * window from closing in Dev-C++.  Note that this is only needed when
    * running the resultant executable from within Dev-Cpp itself or via
    * clicking on the file with the mouse pointer
    ***************************************************************************/
    system("PAUSE");
    exit(1);
}

int
main(int argc, char **argv)
{
    time_t t1;
    time_t t2;
    /***************************************************************************
     * mingw time.h doesn't have struct timeval
     * struct timeval tb1;
     * struct timeval tb2;
     **************************************************************************/
    const char *conninfo;
    PGconn *conn;
    PGresult *res;
    int i, j = 0;
    long t_diff = 0;
    char statement[1024];
    char buffer[50];
    /***************************************************************************
     * If the user supplies a parameter on the command line, use it as
     * the conninfo string; otherwise default to setting dbname=template1
     * and using environment variables or defaults for all other connection
     * parameters.
     **************************************************************************/
    if (argc > 1)
    {
        conninfo = argv[1];
    }
    else
    {
        conninfo = "hostaddr = 127.0.0.1 dbname = test user = postgres password = postgres";
    }

    /* Make a connection to the database */
    conn = PQconnectdb(conninfo);

    /* Check to see that the backend connection was successfully made */
    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr,
                "Connection to database failed: %s", PQerrorMessage(conn));
        exit_nicely(conn);
    }

    res = PQexec(conn, "drop table notes");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "drop table command failed: %s", PQerrorMessage(conn));
        PQclear(res);
        /***********************************************************************
         * exit_nicely(conn);
         * commented out so that we don't have to create
         * the table manually before we run
         **********************************************************************/
    }
    PQclear(res);

    res = PQexec(conn, "create table notes(subject text)");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "create table command failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }
    PQclear(res);

    res = PQexec(conn, "BEGIN");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }
    PQclear(res);

    (void) time(&t1);
    /* (void) gettimeofday (&tb1, NULL); */

    for (i = 1; i < 2001; i++)
    {
        sprintf(buffer, "my subject%d", i);
        sprintf(statement,
                "%s %s%s", "insert into notes(subject) values('", buffer, "')");

        res = PQexec(conn, statement);
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
        {
            fprintf(stderr, "insert command failed: %s", PQerrorMessage(conn));
            PQclear(res);
            exit_nicely(conn);
        }
        PQclear(res);
    }
    res = PQexec(conn, "COMMIT");
    PQclear(res);

    (void) time(&t2);
    /* (void) gettimeofday (&tb2, NULL); */
    printf("added %d records to notes\n", i - 1);
    printf("time1: %d time2: %d\n", t1, t2);
    printf("elapsed time: %d\n", (t2 - t1));
    printf("The start time was %s.\n", asctime(localtime(&t1)));
    printf("The end time was %s.\n", asctime(localtime(&t2)));

    /***************************************************************************
    * t_diff = (long) ((tb2.tv_sec - tb1.tv_sec) + (tb2.tv_usec - tb1.tv_usec));
    * printf ("seconds: %ld end_microsecs: %ld start_microsecs %ld microsecs %ld\n",
    * (tb2.tv_sec - tb1.tv_sec), tb2.tv_usec, tb1.tv_usec, tb2.tv_usec - tb1.tv_usec);
    * t_diff = (long) (1000.0 *  (tb2.tv_sec - tb1.tv_sec) + (tb2.tv_usec - tb1.tv_usec));
    * printf("elapsed time millisecs: %ld\n", t_diff);
    * *************************************************************************/

    /* close the connection to the database and cleanup */
    PQfinish(conn);

    /***************************************************************************
    * I added this because it's the 'default' means of preventing the cmd.exe
    * window from closing in Dev-C++.  Note that this is only needed when
    * running the resultant executable from within Dev-Cpp itself or via
    * clicking on the file with the mouse pointer
    ***************************************************************************/
    system("PAUSE");

    return 0;
}

В списке pgsql-general по дате отправления:

Предыдущее
От: "Aman Tur"
Дата:
Сообщение: Connecting form Access or VB6 to PostgreSQL 8
Следующее
От: "A. Kretschmer"
Дата:
Сообщение: Re: Connecting form Access or VB6 to PostgreSQL 8