Compiling and Running the Sample Applications

This section lists the following code samples:

Initialize.h
PrepAndExec.c
ExecDirect.c
ViewTable.c
Transact.c

This section contains the steps required to get the sample code working with Red Hat Database.

  1. If they are not already installed, install the rh-postgresql-odbc and rh-postgresql-devel packages (both are required for this application to work). See the section Installing the ODBC Driver.

  2. Add the ODBC catalog extensions (as user postgres) to template1:

    $ psql -d template1 -f /usr/share/pgsql/odbc.sql
  3. Create a database called basketball using your userid. If your userid is not allowed to create databases, talk to your database administrator or refer to the Red Hat Database Administrator and User's Guide for instructions on how to grant "create database" permission to a user.

  4. Create a table called players within the basketball database, then insert a few records into the table using psql commands described under the Section called Database Schema.

  5. For a standard installation of Red Hat Database, the host access control file is located at /var/lib/pgsql/data/pg_hba.conf. Edit pg_hba.conf to add a record similar to the following:

    host   all   127.0.0.1   255.255.255.255   trust

    Refer to the Red Hat Database Administrator and User's Guide for a proper client authentication record to suit your configuration.

  6. To accept TCP/IP connections, edit the configuration file to allow TCP/IP connections. For a standard installation of Red Hat Database, the configuration file is located at /var/lib/pgsql/data/postgresql.conf. Modify the value of tcpip_socket from false to true to that TCP/IP connections are allowed by default every time postmaster starts up. (Another way to this is by restarting postmaster with the -i option.)

  7. Restart postmaster so that the changes will be effective. You can do that by issuing the following command as user postgres:

    $ pg_ctl restart
  8. Edit the .odbc.ini file and add the following:

    [ODBC Data Sources]
    RHDB = RedHatDatabase
    
    [RHDB]
    Servername = localhost
    Database = basketball
    ReadOnly = No
    CommLog = 1
    Debug = 1
  9. Create files named Initialize.h, PrepAndExec.c, ExecDirect.c, ViewTable.c, and Transact.c. (See the the Section called Code Sample section.)

    Remember to update the DSN definition in Initialize.h for your setting.

  10. Compile the sample code:

    gcc -lpsqlodbc -I/usr/include/pgsql PrepAndExec.c -o PrepAndExec
    gcc -lpsqlodbc -I/usr/include/pgsql ExecDirect.c -o ExecDirect
    gcc -lpsqlodbc -I/usr/include/pgsql ViewTable.c -o ViewTable
    gcc -lpsqlodbc -I/usr/include/pgsql Transact.c -o Transact

    These commands compile the code, link with the ODBC library (psqlodbc.so), and use the include files under the /usr/include/pgsql directory.

  11. Show the contents of the players table as follows:

    $ ViewTable

    You should see the following output:

    Connection Parameters: DSN='RHDB', UID='postgres', PWD='postgres'
    Handles initialized.
     
    Content of players:
    ******************
    Michael Jordan -- Washington Wizards
    Tim Duncan -- San Antonio Spurs
    Vince Carter -- Toronto Raptors
  12. Use the PrepAndExec command to prepare and execute the SQL statement that inserts a record into the players table:

    $ PrepAndExec "Insert into players values('Kobe Bryant', 'Portland Trail Blazers')"

    You can execute ViewTable again to see the updated content of the players table.

  13. Use the ExecDirect command to execute directly the SQL statement that modifies a record in the players table:

    $ ExecDirect "Update players set team='LA Lakers' where name='Kobe Bryant'"

    You can execute ViewTable again to see the updated content of the players table.

  14. Transact.c is an example that contains a transaction with the following SQL statements:

    INSERT into players VALUES ('Tracy McGrady', 'Orlando Magic')
    DELETE FROM players WHERE name='Michael Jordan'

    To execute the SQL statements in the transaction and rollback at the end of the transaction:
    $ Transact 0

    To check that the changes are not applied to the players table.
    $ ViewTable

    To execute the SQL statements in a transaction and commit them at the end of the transaction:
    $ Transact 1

    To see that the modifications are applied to the players table:
    $ ViewTable