MySQL Forums
Forum List  »  Connector/C++

Re: Connector linking problem
Posted by: Manh Nguyen
Date: November 03, 2010 02:17AM

If you're using Windows. Try to get rid of the complicated MySql Connector C++ library.
All you have to do is install the MySql ODBC Connector. And use the classical C library to connect to ODBC datasource.
Here is my code:


/*******************************************************************
*
* DRIVER=MySQL ODBC 5.1 Driver
* UID=root
* PORT=3306
* DATABASE=test_db
* SERVER=localhost
*
******************************************************************/
#include<iostream>
#include<windows.h>
#include<sql.h>
#include<sqlext.h>
#include<sqltypes.h>

#pragma comment(lib,"odbc32.lib")
#pragma comment(lib,"odbccp32.lib")

using namespace std;


int main(int arg,char** argv)
{
SQLHENV sql_hEnv = 0;
SQLHDBC sql_hDBC = 0;
SQLHSTMT sql_hStmt = 0;
SQLCHAR szDNS[1024] ={0};
SQLSMALLINT nSize = 0;

SQLRETURN sqlRet = SQLAllocHandle( SQL_HANDLE_ENV,SQL_NULL_HANDLE,&sql_hEnv );
sqlRet = SQLSetEnvAttr( sql_hEnv,SQL_ATTR_ODBC_VERSION,(void*) SQL_OV_ODBC3,0 );
sqlRet = SQLAllocHandle( SQL_HANDLE_DBC,sql_hEnv,&sql_hDBC );

sqlRet = SQLDriverConnect( sql_hDBC,0,
(SQLCHAR*)"DSN=TestDataSourceName;UID=root;PWD=root;",
SQL_NTS,
szDNS,
1024,
&nSize,
SQL_DRIVER_COMPLETE );


if( SQL_SUCCEEDED( sqlRet ))
{
cout<<"Connected to the database"<<endl;
cout<<"Connection Info:"<<szDNS<<endl;
cout<<endl;
sqlRet = SQLAllocHandle(SQL_HANDLE_STMT,sql_hDBC,&sql_hStmt );
sqlRet = SQLExecDirect( sql_hStmt,(SQLCHAR*)"SELECT * FROM test_db.test_table;",SQL_NTS );
SQLSMALLINT nCols = 0;
SQLINTEGER nRows = 0;
SQLINTEGER nIdicator = 0;
SQLCHAR buf[1024] = {0};
SQLNumResultCols( sql_hStmt, &nCols );
SQLRowCount( sql_hStmt, &nRows );


while( SQL_SUCCEEDED( sqlRet = SQLFetch( sql_hStmt ) ) )
{
for( int i=1; i <= nCols; ++i )
{
sqlRet = SQLGetData( sql_hStmt,i,SQL_C_CHAR,buf,1024,&nIdicator );
if( SQL_SUCCEEDED( sqlRet ))
cout<< buf;
cout<<"\t";
}
cout<<endl;
}

SQLFreeHandle( SQL_HANDLE_STMT, sql_hStmt );
SQLDisconnect( sql_hDBC );
}
else
{
cout<<"Failed to connect to the database"<<endl;
}
SQLFreeHandle( SQL_HANDLE_DBC, sql_hDBC );
SQLFreeHandle( SQL_HANDLE_ENV, sql_hEnv );


getchar();


return 0;
}

Options: ReplyQuote


Subject
Views
Written By
Posted
4131
October 31, 2010 01:44PM
Re: Connector linking problem
1809
November 03, 2010 02:17AM
1838
November 04, 2010 07:30AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.