473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

One SQL and C question

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

Nov 14 '05 #1
6 1204
# 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Leave it to the Catholics to destroy existence.
Nov 14 '05 #2
Thanks a lot

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

Any reason for that?

Nov 14 '05 #3
>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.
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));
}
}
}

Nov 14 '05 #4
po***********@gmail.com 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?

--
SM Ryan http://www.rawbw.com/~wyrmwif/
God's a skeeball fanatic.
Nov 14 '05 #5
QQ
No
if I pass it as a parameter
it fails,

if I define it as a global
it works

feell weired

Nov 14 '05 #6
"QQ" <ju****@yahoo.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.