Q
In C and SQL I write a very simple program and it works
However when I try to create a MYSQL object and call it in the
function,
it fails. Here is my program, what's wrong? Thanks a lot!
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
int main(int argc, char *argv[]) {
MYSQL my_connection;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "localhost", "","", "",
0, NULL, 0))
{
printf("Connection success\n");
db_Roamer_Create("testtest");
}
else
{
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",
mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
mysql_close(&my_connection);
}
int db_Roamer_Create(MYSQL *my_connection,const char *dbname)
{
printf("dbName is %s\n",dbname);
char *cmd;
if ( ( cmd = malloc( strlen( "CREATE DATABASE " ) +
strlen( dbname ) + 1 ) ) == NULL ) {
fprintf( stderr, "Running out of memory!\n" );
exit( EXIT_FAILURE );
}
sprintf(cmd,"CREATE DATABASE %s;",dbname);
if(mysql_query(my_connection,cmd)==0)
{
printf("Create Database sucessfully");
}
else
{
fprintf(stderr, "Create Database failed\n");
if (mysql_errno(my_connection)) {
fprintf(stderr, "Create Database error %d: %s\n",
mysql_errno(my_connection), mysql_error(my_connection));
}
}
}
However when I try to create a MYSQL object and call it in the
function,
it fails. Here is my program, what's wrong? Thanks a lot!
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
int main(int argc, char *argv[]) {
MYSQL my_connection;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "localhost", "","", "",
0, NULL, 0))
{
printf("Connection success\n");
db_Roamer_Create("testtest");
}
else
{
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",
mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
mysql_close(&my_connection);
}
int db_Roamer_Create(MYSQL *my_connection,const char *dbname)
{
printf("dbName is %s\n",dbname);
char *cmd;
if ( ( cmd = malloc( strlen( "CREATE DATABASE " ) +
strlen( dbname ) + 1 ) ) == NULL ) {
fprintf( stderr, "Running out of memory!\n" );
exit( EXIT_FAILURE );
}
sprintf(cmd,"CREATE DATABASE %s;",dbname);
if(mysql_query(my_connection,cmd)==0)
{
printf("Create Database sucessfully");
}
else
{
fprintf(stderr, "Create Database failed\n");
if (mysql_errno(my_connection)) {
fprintf(stderr, "Create Database error %d: %s\n",
mysql_errno(my_connection), mysql_error(my_connection));
}
}
}