connect a MySQL database via ODBC from a C program

connect a MySQL database via ODBC from a C program

am 14.07.2009 17:28:20 von asma bouali

Hello,

I want to connect a MySQL database via ODBC from a C program in Visual
Studio 2008 Windows.

I have Mysql version 4.0.15 (esayphp 1.7)
and mysql-connector-odbc-3.51.19-win32

I tested the data source (ODBC)that I've created with the database and
it connects normally.

the DSN name is : toto
the server name is : localhost
the username : root
the password: vide


Here's a part of my C program:
#include
#include
#include
#include
#include

#include
#include
#include

#include
#include
#include
#include


void main()
{
SQLRETURN retcode;
//char design[20];
//int A,B,C,D;
//char request[100];
//char dsn[20]="toto";
//char uid[20]="root";
//char pwd[20]="";
SQLINTEGER lon,la,lb,lc,ld,taille=SQL_NTS;
SQLCHAR design[20];
SQLHDBC hdbc;
SQLHENV henv;
SQLHSTMT hstmt;


/*Allocate environment handle */
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) //{
/* Set the ODBC version environment attribute */
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);

if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) //{
/* Allocate connection handle */
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);

// if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)

//printf("%s","vrai");
//else
//printf("erreur\n");
// }

//}


// retcode = SQLConnect(hdbc,(SQLCHAR*)dsn,
SQL_NTS,(SQLCHAR*)uid, SQL_NTS,(SQLCHAR*)pwd, SQL_NTS);
retcode=SQLConnect(hdbc,(SQLCHAR *)"toto",SQL_NTS,(SQLCHAR
*)"root",SQL_NTS,(SQLCHAR *)"",SQL_NTS);
printf("%d",retcode);
if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
printf("\n%s","vrai");
else
printf("Erreur sur l'instruction SQLConnect !\\n");

}





After the compilation and execution I get the following message:

retcode = - 1 // it is the value of return of SQLConnect

Error on the SQLConnect instruction Impossible to be connected to the
data source:

1. [

- That is what this error message means?: 1. [

--
MySQL ODBC Mailing List
For list archives: http://lists.mysql.com/myodbc
To unsubscribe: http://lists.mysql.com/myodbc?unsub=gcdmo-myodbc@m.gmane.org

Re: connect a MySQL database via ODBC from a C program

am 15.07.2009 09:39:39 von Martin Evans

asma bouali wrote:
> Hello,
>
> I want to connect a MySQL database via ODBC from a C program in Visual
> Studio 2008 Windows.
>
> I have Mysql version 4.0.15 (esayphp 1.7)
> and mysql-connector-odbc-3.51.19-win32
>
> I tested the data source (ODBC)that I've created with the database and
> it connects normally.
>
> the DSN name is : toto
> the server name is : localhost
> the username : root
> the password: vide
>
>
> Here's a part of my C program:
> #include
> #include
> #include
> #include
> #include
>
> #include
> #include
> #include
>
> #include
> #include
> #include
> #include
>
>
> void main()
> {
> SQLRETURN retcode;
> //char design[20];
> //int A,B,C,D;
> //char request[100];
> //char dsn[20]="toto";
> //char uid[20]="root";
> //char pwd[20]="";
> SQLINTEGER lon,la,lb,lc,ld,taille=SQL_NTS;
> SQLCHAR design[20];
> SQLHDBC hdbc;
> SQLHENV henv;
> SQLHSTMT hstmt;
>
>
> /*Allocate environment handle */
> retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
>
> if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) //{
> /* Set the ODBC version environment attribute */
> retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
>
> if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) //{
> /* Allocate connection handle */
> retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
>
> // if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
>
> //printf("%s","vrai");
> //else
> //printf("erreur\n");
> // }
>
> //}
>
>
> // retcode = SQLConnect(hdbc,(SQLCHAR*)dsn,
> SQL_NTS,(SQLCHAR*)uid, SQL_NTS,(SQLCHAR*)pwd, SQL_NTS);
> retcode=SQLConnect(hdbc,(SQLCHAR *)"toto",SQL_NTS,(SQLCHAR
> *)"root",SQL_NTS,(SQLCHAR *)"",SQL_NTS);
> printf("%d",retcode);
> if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
> printf("\n%s","vrai");
> else
> printf("Erreur sur l'instruction SQLConnect !\\n");
>
> }
>
>
>
>
>
> After the compilation and execution I get the following message:
>
> retcode = - 1 // it is the value of return of SQLConnect
>
> Error on the SQLConnect instruction Impossible to be connected to the
> data source:
>
> 1. [
>
> - That is what this error message means?: 1. [
>

I already replied to this posting when you posted it a few days ago when
I said:

I suggest you call SQLError or preferably, SQLGetDiagRec (as you are
asking for ODBC 3) after the connect call fails to find out what is
wrong with your connect call.

Also, you can use the SQL_SUCCEEDED macro instead of always checking for
SQL_SUCCESS and SQL_SUCCESS_WITH_INFO.

Martin
--
Martin J. Evans
Easysoft Limited
http://www.easysoft.com

--
MySQL ODBC Mailing List
For list archives: http://lists.mysql.com/myodbc
To unsubscribe: http://lists.mysql.com/myodbc?unsub=gcdmo-myodbc@m.gmane.org