Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to access a local postgres database from VC++ with ODBC?

How to access a local postgres database from VC++ with ODBC?

Scheduled Pinned Locked Moved Database
databasec++postgresqltutorialworkspace
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    keret
    wrote on last edited by
    #1

    I try to access a local postgres database (actually it is a PokerTracker3 database) from Visual C++ 2008, because I'd like to do a lot of regular analysis, which takes hours of work with PT3, but if I write a standalone program, it can make it for me by a push of a button. I am a slightly advanced C++ programmer, but I'm new to databases. I tried at first with libpqxx, but I couldn't even compile it's test programs. It had configuration problems I couldn't solve. Then I found out about ODBC, and I try this for now. So far I updated postgres to 8.3.9 with psqlodbc 8.4 (that's what was automatically installed with it). I found an example on the net with an access database here: http://www.dmcmsp.com/newHome/odbc/cplusplus/index.htm I tried to amend it to work with postgreSQL. At first, I tried to access the lookup_hand_groups table. Server name is postgres, database name is PT3_2010, username is postgres, password is dbpass. It's a console application (later I will write the reports to a csv file), that's what I have so far:

    #include "stdafx.h"
    #include "windows.h"
    #include <sqlext.h>

    int main(int argc, char\* argv\[\])
    {
    printf("Starting\\r\\n");
    
    SQLHANDLE hEnv; // ODBC Environment handle
    SQLHANDLE hDbc; // ODBC Connection handle
    SQLHANDLE hStmt; // ODBC Statement handle
    
    SQLRETURN status; // SQL return variable to test success or failure
    
    // 1. Create Environment Handle
    status = SQLAllocHandle(SQL\_HANDLE\_ENV, SQL\_NULL\_HANDLE, &hEnv);
    
    // 2. Set the ODBC Version we are using to 3.0
    status = SQLSetEnvAttr(hEnv, SQL\_ATTR\_ODBC\_VERSION, (void \*) SQL\_OV\_ODBC3, 0);
    
    // 3. Create a Connection Handle.
    status = SQLAllocHandle(SQL\_HANDLE\_DBC, hEnv, &hDbc);
    
    // 4. Set Connection Login Timeout to 5 seconds.
    status = SQLSetConnectAttr(hDbc, SQL\_LOGIN\_TIMEOUT,(void \*) 5, 0);
    
    // 5. Open a Connection To Datasource (DSN
    status = SQLConnect(hDbc,(SQLWCHAR \*) "postgres", SQL\_NTS,
    (SQLWCHAR \*) "postgres", SQL\_NTS,
    (SQLWCHAR \*) "dbpass", SQL\_NTS); // connect to DSN
    
    // 6. Create SQL Statement Handle.
    status = SQLAllocHandle(SQL\_HANDLE\_STMT, hDbc, &hStmt);
    
    // 7. Set Cursor Type to Static.
    status = SQLSetStmtAttr(hStmt, SQL\_ATTR\_CURSOR\_TYPE, (SQLPOINTER) SQL\_CURSOR\_STATIC, 0);
    
    // 8. Execute a SQL Statement. Here we get the employee records from Northwind database.
    status = SQLExecDirect(hStm
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups