Connecter au Mysql via ODBC à partir d"un programme

Connecter au Mysql via ODBC à partir d"un programme

am 09.07.2009 23:41:56 von Rim msedi

--0015174be8748e3016046e4cb992
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Salut,

Je voudrais connecter =E0 une base de donn=E9es Mysql via ODBC =E0 partir d=
'un
programme C sous Visual Studio 2008 Windows.
J'ai Mysql version 4.0.15 (esayphp 1.7)
et mysql-connector-odbc-3.51.19-win32

j'ai test=E9 la source de donn=E9es (ODBC)qui j'ai ajout=E9 avec la base d=
e
donn=E9es. ceci se passe bien.

le nom de DSN est : toto
le nom d'utilisateur : root
le mot de passe est vide


Voici une partie de mon programme C:


#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]=3D"toto";
//char uid[20]=3D"root";
//char pwd[20]=3D"";
SQLINTEGER lon,la,lb,lc,ld,taille=3DSQL_NTS;
SQLCHAR design[20];
SQLHDBC hdbc;
SQLHENV henv;
SQLHSTMT hstmt;


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

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

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

// if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_I=
NFO)

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

//}


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


SQLAllocStmt ( hdbc, &hstmt);
SQLBindCol(hstmt,1,SQL_C_CHAR,design,sizeof(design),&lon);
SQLBindCol(hstmt,2,SQL_C_ULONG,&A,0,&la);
SQLBindCol(hstmt,3,SQL_C_ULONG,&B,0,&lb);
SQLBindCol(hstmt,4,SQL_C_ULONG,&C,0,&lc);
SQLBindCol(hstmt,5,SQL_C_ULONG,&D,0,&ld);
//SQLExecDirect(hstmt,(UCHAR FAR *)"select * from connaissance",SQL_NTS);
strcpy(request,"select * from ");
strcat(request,nom_table);
strcat(request,";");
//strcat(request," where designation like ?");
//SQLPrepare(hstmt,"select * from connaidiabete where designation like
?",SQL_NTS);
SQLExecDirect(hstmt,(UCHAR FAR *)request,SQL_NTS);
//SQLPrepare(hstmt,request."where designation like ?",SQL_NTS);
//SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,
SQL_VARCHAR,20,0,valeur,0,&taille);
//SQLExecute(hstmt);

while (SQLFetch(hstmt)!=3DSQL_NO_DATA)
{

if(strcmp(valeur,design)==0)
printf("oui\n");

}
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
SQLDisconnect(hdbc);

//SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
//SQLFreeHandle(SQL_HANDLE_ENV,henv);
}


Apr=E8s la compilation et l'ex=E9cution j'aurais ce message:
Erreur sur l'instruction SQLConnect !\n

C'est =E0 dire la connexion ne passe pas bien.

j'ai consult=E9 la documentation sur la chaine de connexion dans visual stu=
dio
2008 dans le
lien suivant. Mais je ne trouve pas une solution.

http://msdn.microsoft.com/en-us/library/ms711810(VS.85).aspx

Comment ce probl=E8me de connexion va =EAtre r=E9solue?
Avez-vous un exemple de test pour tester ma connexion et traiter quelques
requ=EAtes ?

Le reste de travail se base sur la r=E9ussite du connexion au base des
donn=E9es et l'ex=E9cution des requ=EAtes.
SVP, j'ai besoin de votre aide.

A++

--0015174be8748e3016046e4cb992--

Re: Connecter au Mysql vi

am 10.07.2009 00:30:30 von Jess Balint

Hi, I don't speak French. If you have a problem with SQLConnect(),
try calling SQLGetDiagField()/SQLGetDiagRec() to obtain the reason it
failed.

If you can briefly describe your problem in English, I can offer more
assistance.

Best,
Jess

--
Jess Balint
Sun Microsystems, Inc
Office: +1 218 206 2477

On Thu, Jul 09, 2009 at 10:41:56PM +0100, Rim msedi wrote:
> Salut,
>
> Je voudrais connecter à une base de données Mysql via ODBC à partir d'un
> programme C sous Visual Studio 2008 Windows.
> J'ai Mysql version 4.0.15 (esayphp 1.7)
> et mysql-connector-odbc-3.51.19-win32
>
> j'ai testé la source de données (ODBC)qui j'ai ajouté avec la base de
> données. ceci se passe bien.
>
> le nom de DSN est : toto
> le nom d'utilisateur : root
> le mot de passe est vide
>
>
> Voici une partie de mon programme C:
>
>
> #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);
> if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
> printf("%s","vrai");
> else
> printf("Erreur sur l'instruction SQLConnect !\\n");
>
>
> SQLAllocStmt ( hdbc, &hstmt);
> SQLBindCol(hstmt,1,SQL_C_CHAR,design,sizeof(design),&lon);
> SQLBindCol(hstmt,2,SQL_C_ULONG,&A,0,&la);
> SQLBindCol(hstmt,3,SQL_C_ULONG,&B,0,&lb);
> SQLBindCol(hstmt,4,SQL_C_ULONG,&C,0,&lc);
> SQLBindCol(hstmt,5,SQL_C_ULONG,&D,0,&ld);
> //SQLExecDirect(hstmt,(UCHAR FAR *)"select * from connaissance",SQL_NTS);
> strcpy(request,"select * from ");
> strcat(request,nom_table);
> strcat(request,";");
> //strcat(request," where designation like ?");
> //SQLPrepare(hstmt,"select * from connaidiabete where designation like
> ?",SQL_NTS);
> SQLExecDirect(hstmt,(UCHAR FAR *)request,SQL_NTS);
> //SQLPrepare(hstmt,request."where designation like ?",SQL_NTS);
> //SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,
> SQL_VARCHAR,20,0,valeur,0,&taille);
> //SQLExecute(hstmt);
>
> while (SQLFetch(hstmt)!=SQL_NO_DATA)
> {
>
> if(strcmp(valeur,design)==0)
> printf("oui\n");
>
> }
> SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
> SQLFreeHandle(SQL_HANDLE_ENV,henv);
> SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
> SQLDisconnect(hdbc);
>
> //SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
> //SQLFreeHandle(SQL_HANDLE_ENV,henv);
> }
>
>
> Après la compilation et l'exécution j'aurais ce message:
> Erreur sur l'instruction SQLConnect !\n
>
> C'est à dire la connexion ne passe pas bien.
>
> j'ai consulté la documentation sur la chaine de connexion dans visual studio
> 2008 dans le
> lien suivant. Mais je ne trouve pas une solution.
>
> http://msdn.microsoft.com/en-us/library/ms711810(VS.85).aspx
>
> Comment ce problème de connexion va être résolue?
> Avez-vous un exemple de test pour tester ma connexion et traiter quelques
> requêtes ?
>
> Le reste de travail se base sur la réussite du connexion au base des
> données et l'exécution des requêtes.
> SVP, j'ai besoin de votre aide.
>
> A++

--
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: Connecter au Mysql via ODBC à partir d"un programme C sous Visual Studio 2008 Windows

am 10.07.2009 19:26:02 von mrodriguez

Hi, I hope my french-english translation is coherent enough, but what
Rim msedi said was something like:

"
Hello,

I want to connect a MySQL database via ODBC fron 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 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);
if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
printf("%s","vrai");
else
printf("Erreur sur l'instruction SQLConnect !\\n");


SQLAllocStmt ( hdbc, &hstmt);
SQLBindCol(hstmt,1,SQL_C_CHAR,design,sizeof(design),&lon);
SQLBindCol(hstmt,2,SQL_C_ULONG,&A,0,&la);
SQLBindCol(hstmt,3,SQL_C_ULONG,&B,0,&lb);
SQLBindCol(hstmt,4,SQL_C_ULONG,&C,0,&lc);
SQLBindCol(hstmt,5,SQL_C_ULONG,&D,0,&ld);
//SQLExecDirect(hstmt,(UCHAR FAR *)"select * from connaissance",SQL_NTS);
strcpy(request,"select * from ");
strcat(request,nom_table);
strcat(request,";");
//strcat(request," where designation like ?");
//SQLPrepare(hstmt,"select * from connaidiabete where designation like
?",SQL_NTS);
SQLExecDirect(hstmt,(UCHAR FAR *)request,SQL_NTS);
//SQLPrepare(hstmt,request."where designation like ?",SQL_NTS);
//SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,
SQL_VARCHAR,20,0,valeur,0,&taille);
//SQLExecute(hstmt);

while (SQLFetch(hstmt)!=SQL_NO_DATA)
{

if(strcmp(valeur,design)==0)
printf("oui\n");

}
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
SQLDisconnect(hdbc);

//SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
//SQLFreeHandle(SQL_HANDLE_ENV,henv);
}


After the compilation and execution I get the following message:
Error in the SQLConnect instruction !\n

Meaning the connection does not go through.

I've consulted the documentation about the connection string on visual studio 2008 on the following link. But I did not find a solution.

http://msdn.microsoft.com/en-us/library/ms711810(VS.85).aspx

How can this problem be resolved?
Have you a testing example for trying my conection and
Avez-vous un exemple de test pour tester ma connexion et try some queries?

The rest of work is based on the results of the connection to the database and the execution of queries. Le reste de travail se base sur la réussite du connexion au base des

Please I need your help.

A++
"

Now, the rest is up to the C - MySQL gurus ... (my field is not C ...
sorry about that!)

Cheers.

Mario F. Rodríguez
Highland Systems Xela
Quetzaltenango, Guatemala

--
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: Connecter au Mysql via ODBC à partir d"un programme C sous Visual Studio 2008 Windows

am 13.07.2009 11:06:23 von Martin Evans

Mario Fernando Rodríguez wrote:
> Hi, I hope my french-english translation is coherent enough, but what
> Rim msedi said was something like:
>
> "
> Hello,
>
> I want to connect a MySQL database via ODBC fron 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 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);
> if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
> printf("%s","vrai");
> else
> printf("Erreur sur l'instruction SQLConnect !\\n");
>
>
> SQLAllocStmt ( hdbc, &hstmt);
> SQLBindCol(hstmt,1,SQL_C_CHAR,design,sizeof(design),&lon);
> SQLBindCol(hstmt,2,SQL_C_ULONG,&A,0,&la);
> SQLBindCol(hstmt,3,SQL_C_ULONG,&B,0,&lb);
> SQLBindCol(hstmt,4,SQL_C_ULONG,&C,0,&lc);
> SQLBindCol(hstmt,5,SQL_C_ULONG,&D,0,&ld);
> //SQLExecDirect(hstmt,(UCHAR FAR *)"select * from connaissance",SQL_NTS);
> strcpy(request,"select * from ");
> strcat(request,nom_table);
> strcat(request,";");
> //strcat(request," where designation like ?");
> //SQLPrepare(hstmt,"select * from connaidiabete where designation like
> ?",SQL_NTS);
> SQLExecDirect(hstmt,(UCHAR FAR *)request,SQL_NTS);
> //SQLPrepare(hstmt,request."where designation like ?",SQL_NTS);
> //SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,
> SQL_VARCHAR,20,0,valeur,0,&taille);
> //SQLExecute(hstmt);
>
> while (SQLFetch(hstmt)!=SQL_NO_DATA)
> {
>
> if(strcmp(valeur,design)==0)
> printf("oui\n");
>
> }
> SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
> SQLFreeHandle(SQL_HANDLE_ENV,henv);
> SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
> SQLDisconnect(hdbc);
>
> //SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
> //SQLFreeHandle(SQL_HANDLE_ENV,henv);
> }
>
>
> After the compilation and execution I get the following message:
> Error in the SQLConnect instruction !\n
>
> Meaning the connection does not go through.
>
> I've consulted the documentation about the connection string on visual
> studio 2008 on the following link. But I did not find a solution.
>
> http://msdn.microsoft.com/en-us/library/ms711810(VS.85).aspx
>
> How can this problem be resolved?
> Have you a testing example for trying my conection and Avez-vous un
> exemple de test pour tester ma connexion et try some queries?
>
> The rest of work is based on the results of the connection to the
> database and the execution of queries. Le reste de travail se base sur
> la réussite du connexion au base des
>
> Please I need your help.
>
> A++
> "
>
> Now, the rest is up to the C - MySQL gurus ... (my field is not C ...
> sorry about that!)
>
> Cheers.
>
> Mario F. Rodríguez
> Highland Systems Xela
> Quetzaltenango, Guatemala
>

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