Rak111 | Pour se connecter a une base de données et lancer des requetes, voila la demarche a suivre en C
Code :
- #include <mysql/mysql.h>
- MYSQL *conn =NULL;
- MYSQL *erreur;
- MYSQL_RES *res;
- MYSQL_ROW row;
- const char *host;
- const char *user;
- const char *passwd;
- const char *db;
- unsigned int port;
- const char *unix_socket;
- unsigned long client_flag = 0;
- int t;
- int r;
- host = "127.0.0.1"; //url
- user = "root"; //user id
- passwd = NULL; //password
- db = "david"; //pool de database
- port = 3306; //port
- unix_socket = NULL;
- char query[100] = "select ... from ... "; //requete SQL
- /*initialisation de la connection*/
- if((conn = mysql_init(NULL))==NULL)
- {
- printf("Not enough memory\n" );
- exit(-1);
- }
- /*connection a la database*/
- if((erreur=mysql_real_connect(conn,host,user,passwd,db,port,unix_socket,client_flag))==NULL)
- {
- printf("Server connexion failed\n" );
- printf("cause : %s\n",mysql_error(conn));
- exit(1);
- }
- /*lancement de la requete*/
- t=mysql_real_query(conn,query,(unsigned int) strlen(query));
- if (t)
- {
- printf("Error making query: %s\n",
- mysql_error(conn));
- }
- /*recuperation du resultat*/
- res=mysql_use_result(conn);
-
- /*parcours des resultats*/
- for(r=0;r<mysql_field_count(conn);r++){
- row=mysql_fetch_row(res);
- if(row<0) break;
- for(t=0;t<mysql_num_fields(res);t++){
- printf("%s",row[t]);
- }
- }
- mysql_close(conn);
- }
|
---------------
-= RAK =-
|