Click here to Skip to main content
15,912,400 members
Home / Discussions / Database
   

Database

 
AnswerRe: Hiding the Database Pin
Eddy Vluggen2-Feb-10 4:09
professionalEddy Vluggen2-Feb-10 4:09 
AnswerRe: Hiding the Database Pin
Ashfield2-Feb-10 8:54
Ashfield2-Feb-10 8:54 
AnswerRe: Hiding the Database Pin
PIEBALDconsult2-Feb-10 14:06
mvePIEBALDconsult2-Feb-10 14:06 
QuestionNeed to import a table & Query from one mdb to other Pin
honeyashu1-Feb-10 16:43
honeyashu1-Feb-10 16:43 
AnswerRe: Need to import a table & Query from one mdb to other Pin
_Damian S_1-Feb-10 17:15
professional_Damian S_1-Feb-10 17:15 
GeneralRe: Need to import a table & Query from one mdb to other Pin
honeyashu1-Feb-10 17:19
honeyashu1-Feb-10 17:19 
GeneralRe: Need to import a table & Query from one mdb to other Pin
Mycroft Holmes1-Feb-10 19:15
professionalMycroft Holmes1-Feb-10 19:15 
QuestionHow to access a local postgres database from VC++ with ODBC? Pin
keret1-Feb-10 12:19
keret1-Feb-10 12:19 
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(hStmt,(SQLWCHAR *) "Select * from lookup_hand_groups", SQL_NTS);

   // 9. Do whatever you will with the returned data. First, check to see that something
   // was returned.

   printf("After settings\r\n");
   // count the number of rows in the returned data
   int rowcnt; // variable to hold row count

   rowcnt = 0; // initialize row counter

   while (true) // loop forever
   {
   status = SQLFetch(hStmt); // fetch a row of data

   if (status == SQL_NO_DATA_FOUND) break; // if No Data was returned then exit the loop

   rowcnt++; // add 1 to the row counter
   }
   printf("rowcnt\r\n");

   printf("There were %d rows of data returned by the SQL Statement.\r\n", rowcnt);

   // Now, reset the cursor to the first row of data

   status = SQLFetchScroll(hStmt,SQL_FETCH_FIRST,0);

   if (rowcnt > 0) {
   SQLSMALLINT numcols; // variable to store number of SQL Columns

   status = SQLNumResultCols(hStmt, &numcols); // retrieve number of SQL Columns

   if (status == SQL_SUCCESS || status == SQL_SUCCESS_WITH_INFO) {
   printf("Number of returned Columns is: %d. \r\n",numcols);
   }
   else {
   printf("Error returning number of Columns...\r\n");
   }

   // Describe the Name of a Column in the returned data

   # define SQL_MAX_NAME_SIZE 128

   SQLSMALLINT iCol; // variable to hold column number to Describe

   iCol = 3; // Column Number to Describe

   SQLWCHAR namebuffer[SQL_MAX_NAME_SIZE + 1]; // buffer to receive column name

   SQLSMALLINT buffersize; // returns actual size of column name returned

   SQLSMALLINT typeOfData; // returns type of data in the column

   SQLUINTEGER columnsize; // returns size of the column

   SQLSMALLINT decimaldigits; // returns number of decimal digits for the column

   SQLSMALLINT nullable; // returns - does column allow NULLs? 1 = yes, 0 = No

   memset(namebuffer, 0, SQL_MAX_NAME_SIZE + 1); // initialize name buffer

   status = SQLDescribeCol(hStmt, iCol, namebuffer, SQL_MAX_NAME_SIZE, &buffersize, &typeOfData, &columnsize,
   &decimaldigits, &nullable); // Get Description of Column

   printf("Column name of column number %d is %s.\r\n",iCol, namebuffer);


   // Last, but not least, return the Column Data from the column number iCol shown above.

   int icol = iCol; // same column number as above
   long nchars; // number of characters returned

   char buffer[512]; // 512 character buffer

   memset(buffer,0,sizeof(buffer)); // initialize buffer

   // Get the Column Data and convert it to Character.
   status = SQLGetData(hStmt, icol, SQL_C_CHAR, buffer, sizeof(buffer) - 1, &nchars);

   printf("Data from Column %d is '%s'.\r\n",icol, buffer);
   }

   // 10. Close the Cursor if one was created...
   status = SQLCloseCursor(hStmt);

   if (status == SQL_SUCCESS || status == SQL_SUCCESS_WITH_INFO) {
   }
   else {
   printf("Error with SQLCloseCursor...\r\n");
   }

   // 11. Release the SQL Statement Handle.
   status = SQLFreeHandle(SQL_HANDLE_STMT, hStmt);

   // 12. Close the Connection
   status = SQLDisconnect(hDbc);

   // 13. Release the Connection Handle
   status = SQLFreeHandle(SQL_HANDLE_DBC, hDbc);

   // 14. Release the Environment Handle
   status = SQLFreeHandle(SQL_HANDLE_ENV, hEnv);

   printf("All Done!\n\nPress Any Key To Continue...");

   getchar(); // wait for a keypress -- not required in Visual Studio 6 but is in later ones

   return 0;
   }


This tutorial mentions an ODBC32.lib, what I should add to the project link options, but I couldn't find it. The above program cannot connect to the server. At #5 status is -1. What should I write to #5? In the tutorial it says that I should define an ODBC DSN, but I don't have a clue, how to do it. Could you point me to the right direction? Maybe to a psql-odbc-VC++2008 step by step tutorial for dummies? Smile | :)
Questionsql instances and a deuced connection Pin
reza assar1-Feb-10 10:38
reza assar1-Feb-10 10:38 
AnswerRe: sql instances and a deuced connection Pin
Eddy Vluggen1-Feb-10 11:15
professionalEddy Vluggen1-Feb-10 11:15 
Questionsql table name Pin
jashimu1-Feb-10 6:00
jashimu1-Feb-10 6:00 
AnswerRe: sql table name Pin
David Mujica1-Feb-10 6:16
David Mujica1-Feb-10 6:16 
GeneralRe: sql table name Pin
jashimu1-Feb-10 7:22
jashimu1-Feb-10 7:22 
QuestionCompare Table Structure in SQL Server 2005 Pin
Isaac Gordon31-Jan-10 19:43
Isaac Gordon31-Jan-10 19:43 
AnswerRe: Compare Table Structure in SQL Server 2005 Pin
Mycroft Holmes1-Feb-10 0:06
professionalMycroft Holmes1-Feb-10 0:06 
QuestionSQL Compact Pin
Mycroft Holmes31-Jan-10 18:32
professionalMycroft Holmes31-Jan-10 18:32 
AnswerRe: SQL Compact Pin
i.j.russell31-Jan-10 22:05
i.j.russell31-Jan-10 22:05 
Questionplease help me figure out why the heck i can't change the primary key or drop the table in sql 2008 express Pin
tonyonlinux31-Jan-10 10:47
tonyonlinux31-Jan-10 10:47 
AnswerRe: please help me figure out why the heck i can't change the primary key or drop the table in sql 2008 express Pin
Mycroft Holmes31-Jan-10 12:15
professionalMycroft Holmes31-Jan-10 12:15 
GeneralRe: please help me figure out why the heck i can't change the primary key or drop the table in sql 2008 express Pin
tonyonlinux31-Jan-10 13:20
tonyonlinux31-Jan-10 13:20 
GeneralRe: please help me figure out why the heck i can't change the primary key or drop the table in sql 2008 express Pin
Mycroft Holmes31-Jan-10 13:30
professionalMycroft Holmes31-Jan-10 13:30 
QuestionDo SQL server have some functions as like "Select bottom"? Pin
caiguosen30-Jan-10 21:10
caiguosen30-Jan-10 21:10 
AnswerRe: Do SQL server have some functions as like "Select bottom"? Pin
i.j.russell30-Jan-10 22:44
i.j.russell30-Jan-10 22:44 
AnswerRe: Do SQL server have some functions as like "Select bottom"? Pin
Mycroft Holmes31-Jan-10 0:00
professionalMycroft Holmes31-Jan-10 0:00 
AnswerRe: Do SQL server have some functions as like "Select bottom"? Pin
dan!sh 31-Jan-10 0:14
professional dan!sh 31-Jan-10 0:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.