Friday 22 January 2010

Mysql and C API

//Just a simple example to show that we can work with mysql using c language

// Happy programming
#include
#include
#include

static char *opt_host_name = “yourip”; //eg 192.168.0.254
static char *opt_user_name = “userdatabase”;
static char *opt_password = “userpassword”;
static unsigned int opt_port_num = 3306;
static char *opt_socket_name = NULL;
static char * opt_db_name = “dbname”;
static unsigned int opt_flags = 0;

static MYSQL *conn;

int main(int argc, char *argv[])
{
conn = mysql_init(NULL);

if(conn == NULL)
{
fprintf(stderr,”mysql init() failed\n”);
exit(1);
}

if(mysql_real_connect(conn,opt_host_name,opt_user_name,opt_password,

opt_db_name,opt_port_num,opt_socket_name,opt_flags) == NULL)
{
fprintf(stderr,”mysql_real_connect() failed\n”);
mysql_close(conn);
exit(1);

}
else
{
printf(“connected to mysql server on:%s\t”,opt_host_name);
}

mysql_close(conn);
return 0;
}

No comments:

Post a Comment