One SQL and C question

Q

QQ

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));
}
}
}
 
S

SM Ryan

# 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);

(1) Your malloc is one character short. You need +1 for the ";" and +1 for the
string terminator. My way of doing this would be
static char F[] = "CREATE DATABASE %s;";
char *f = malloc(sizeof F + strlen(dbname));
sprintf(f,F,dbname);

(2) HOWEVER--Don't include the ';' terminator. That's an artifact of marking
the end of the command when read from a terminal. You don't use it when you
call mysql_query.
 
P

poison.summer

Thanks a lot

However, my program works if I define MYSQL my_connection
to be a global variable.

Any reason for that?
 
G

Gordon Burditt

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.

There is no place in your program where it can print "it fails".
*HOW* does it fail? You deserve to have it fail in mysql_real_connect()
for not putting a password on your database, even if it is accessible
only to localhost. You are also obviously out of disk space.

You call db_Roamer_Create with *one* argument, yet define it with
two. Why? Didn't your compiler warn you about that?
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 ) {

You malloc() insufficient memory here for what you want to put in it.
 
S

SM Ryan

(e-mail address removed) wrote:
# Thanks a lot
#
# However, my program works if I define MYSQL my_connection
# to be a global variable.

Are you passing in the value if it's a parameter?
 
Q

QQ

No
if I pass it as a parameter
it fails,

if I define it as a global
it works

feell weired
 
K

Keith Thompson

QQ said:
No
if I pass it as a parameter
it fails,

if I define it as a global
it works

feell weired

What???

Please don't assume that your readers have easy access to the article
to which you're replying. Provide some context.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top