Ver Mensaje Individual
  #5 (permalink)  
Antiguo 08/02/2009, 13:48
Tikitios
 
Fecha de Ingreso: mayo-2008
Ubicación: Chile
Mensajes: 189
Antigüedad: 16 años, 9 meses
Puntos: 3
Respuesta: Como conectar C++ con MySql

Aqui hay un ejemplo:
Cita:
#ifdef WIN32
#include <windows.h>
#include <winsock.h>
#pragma warning (disable: 4514 4786)
#pragma warning( push, 3 )
#endif

#include <stdio.h>
#include "mysql/mysql.h"
#ifndef WIN32
#include <unistd.h>
#endif
#include <iostream>

int main(int argc, char **argv)
{

MYSQL mysql;

printf("Ejemplo conexion mySql");


if(mysql_init(&mysql)==NULL)
{
printf("\nFailed to initate MySQL connection");
exit(1);
}

/*now you can call any MySQL API function you like*/

if (!mysql_real_connect(&mysql,"localhost","tsuari o","la_password","la_database",0,NULL,0))
/*variation #1*/

{

printf( "Failed to connect to MySQL: Error: %s\n", mysql_error(&mysql));
system("PAUSE");
exit(1);

}

printf("Logged on to database sucessfully\n");
system("PAUSE");

mysql_close(&mysql);

}
Es solo un miserable ejemplo ya que no hace nada, solo se mete a la database.
Por supuesto debes linkear -lmysql(para dev)
Y al ejecutar el programa debes tener tu programa mySql(como el xampp) abierto y funcionando mySql o te puede dar error al ejecutar