Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with odbc.

Started by Florian22222 Nov 21, 2011 at 7:48 AM 0 replies 1k views
Original Post
Florian22222
Florian22222
[font="Arial"][color="#1C2837"]I want to connect to a db via c++
[color="#1C2837"]

this is my current code and i have it from

http://cpp.canbal.co...ection-134.html[color="#1C2837"]
[/font]

[font="Arial"] [/font]

[font="Arial"][color="#1c2837"][/font]

[font="Arial"][color="#1c2837"]
#include



#include

#include
#include
#include

int main(void)
{
SQLHANDLE hdlEnv, hdlConn, hdlStmt, hdlDbc;
char* stmt = "SELECT * FROM PLAYERS"; //SQL statement? NutHead is the Table name

//for example
char *dsnName = "Collector"; //data source name?? - how to configure??
char* userID = "Flo";
char* passwd = "wobido";
char* retVal[256];
unsigned int cbData;

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hdlEnv);
SQLSetEnvAttr(hdlEnv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC30);
SQLAllocHandle(SQL_HANDLE_DBC, hdlEnv, &hdlConn);
SQLConnect(hdlConn, (SQLCHAR*)dsnName,SQL_NTS,(SQLCHAR*)userID,SQL_NTS, (SQLCHAR*)passwd, SQL_NTS);
SQLAllocHandle(SQL_HANDLE_STMT, hdlDbc, &hdlStmt);
SQLExecDirect(hdlStmt, (SQLCHAR*)stmt, SQL_NTS);

//Initialize the database connection

while(SQL_SUCCEEDED(SQLFetch(hdlStmt))) //SQL_SUCCEEDED is function not value
{
SQLGetData(hdlStmt,0,DT_STRING,retVal,256,(SQLINTEGER *)&cbData);
std::cout << retVal << std::endl;
}
SQLFreeHandle(SQL_HANDLE_STMT, hdlStmt);
SQLFreeHandle(SQL_HANDLE_DBC, hdlConn);
SQLFreeHandle(SQL_HANDLE_ENV, hdlEnv); //End the connection


}[/font]

[font="Arial"][color="#1c2837"]
[/font]

[font="Arial"] [/font]

[font="Arial"] [/font]

[color="#1C2837"][font="Arial"]i dont know how to handle and i just cant find any useful information about this. i hope you can help me. [/font]
LorenzoGatti
LorenzoGatti
What's the question? There are some obvious errors, but the only thing you seem unable to handle is stray markup in your post.
  • No error checking.
  • char* retVal[256]; is an array of 256 pointers to chars, which is unlikely to be what you need. (It might be accepted as a SQLGetData parameter if it becomes a void pointer along the way, and survive without a segfault because 256 pointers are larger than 256 chars.)
  • You call SELECT * FROM PLAYERS, then you access the first column of the result: how can you know for sure what column it is without tedious calls to SQLColAttribute or the like? Select only that column instead.
Omae Wa Mou Shindeiru

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.