Talkback for article: 304, September2003

The MySQL C API

Back to: http://cgi.linuxfocus.org/English/September2003/article304.shtml

From: df <njpig(at)21cn.com> [ date: 2003-09-11 ]
hi, I love linuxfocus!
I think the example code should be:

#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string.h>

int main(){
MYSQL mysql; /* important */
MYSQL_RES *res;
MYSQL_ROW row;
char *query=(char *)malloc(100);
int t;

mysql_init(&mysql);
if (!mysql_real_connect(&mysql,"localhost","mysql",
"mysql","deneme",0,NULL,0))
{
printf( "Error connecting to database: %s\n",mysql_error(&mysql));
}
else printf("Connected...\n");

strncpy(query,"select * from Deneme", strlen("select * from Deneme"));

t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if (t)
{
printf("Error making query: %s\n",
mysql_error(&mysql));
}
else printf("Query made...\n");
res = mysql_store_result(&mysql);
while(1){
row = mysql_fetch_row(res);
if(row == NULL) break;
for(t=0; t < mysql_num_fields(res); t++){
printf("%s ",row[t]);
}
printf("\n");
}
mysql_close(&mysql);
free(query);
exit(0);
}
From: Brad <brad(at)missioncriticalenteprises.com> [ date: 2003-11-10 ]
I find lots on how to handle geting data out of the SQL database from the c api, both nothing on how to put data in...is this possible?
Any help would be appreciated.
Brad Bowen
From: Boris Sokol <cgifalco(at)cgifalcon.com> [ date: 2003-11-28 ]
Hi,

I was very pleased to read your article, but sorry, when I tried to compile your code under Fedora Core 1, there were run time errors, so I wrote my version of your program and tested it on the mysql database.

There are two main distinctions:

1) definition and using of *connection and mysql
2) and test if(row == NULL) you have if(row < 0)

#include <mysql/mysql.h>
#include <iostream>

using namespace std;

int main(){

MYSQL *connection, mysql;
mysql_init(&mysql);
connection = mysql_real_connect(&mysql,"localhost","root",
"","mysql",0,NULL,0);

if (connection == NULL) {
cout << "Error connecting to database" << mysql_error(connection) << endl;
return 1;
}
else
cout << "Connected...\n";

char *query ="select * from user";

int t;
t=mysql_real_query(connection,query,(unsigned int) strlen(query));

if (t != 0) {
cout << "Error making query" << mysql_error(connection) << endl;
mysql_close(connection);
return 1;
}
else
cout << "Query made...\n";

MYSQL_RES *res;
res=mysql_use_result(connection);

MYSQL_ROW row;
for(int r=0;r<mysql_field_count(connection);r++){
row=mysql_fetch_row(res);
if(row == NULL)
break;
for(t=0;t<mysql_num_fields(res);t++)
cout << row[t];
cout << endl;
}
mysql_close(connection);
return 0;
}

I compiled it in this way:

g++ -o test02 -I/usr/include/mysql test02.cpp -L/usr/lib/mysql -lmysqlclient

Best regards
cgifalco
From: Kayra Otaner <kayraotaner(at)yahoo.com> [ date: 2003-11-28 ]
Nice article, MySQL C Api needs to be more used by developers to use MySQL more effectively, I believe this article is one of the few articles on the net which teachs some basics of the api.

Congrats.

From: bourne <bourne(at)freemail.hu> [ date: 2003-12-04 ]
Hi there!

Thanks for writing tutorials.
Just a suggestion:

mysql = mysql_init(NULL);

Your version caused segmentation fault.
Bye.

From: Baudot Guillaume <guillaume.baudot(at)caramail.com> [ date: 2004-01-19 ]
@brad:
The example program uses only SELECT queries. If you want to modify your
database, then try with INSERT|UPDATE|DELETE queries. There is nothing to change in the API call: you are still sending a request to the database...
Then, if you know a little about SQL, you should not encounter any difficulty. But you can ask me if you need help !..
Regards.
Guillaume Baudot


From: Robert Synnott [ date: 2004-04-12 ]
As to how to put stuff into the DB, here's a tutorial that covers that:
http://www.synnottsoftware.com/tutorials/mysql.html
From: Johnny <collinsj(at)yahoo.com> [ date: 2004-04-12 ]
How do you move the data from the results row from something like
printf("%s ",row[t]); //Simply prints the row
to something to extract the various informaton like,
strcpy (myname, row[0]); //Actually take the info and put it somewhere
I'm having a heck of a time trying to do this. I've figured out you can get string info out however getting numeric data is something I haven't figured out.
Thanks,
John

From: sejaul Haque <sejaul(at)rediffmail.com> [ date: 2004-05-20 ]
Hello Sir , I found ur tutorial very interesting. While compiling the above code i am getting some error like:-

Undefined reference to mysq_real_connet. I compile with the command as

gcc -o test2 -I/usr/include/mysql test2.c -L/usr/lib/mysql
I have Linux 7.2(redhat)installed with mysql database.

Can u pls help me on this by sending mail into my account. If i need to configure mysql lib path or include files path, can u pls tell me that also, how to configure.
Thanks
Sejaul


From: Laurent LEDRU <laurentledru(at)data-mobiles.com> [ date: 2004-08-17 ]
In response to sejaul Haque, I think the problem commes from your version of mysql, you should have 4.0.
I have a question about the SQL DELETE, i made a big Delete (About 500 000 records) using C api, i first make an SELECT * INTO OUTFILE And then A DELETE FROM. It seems that during the 2 query my table or my database is loocked. i have sevral scripts that does nothing during the script i have to stop the query and restart mysql daemon. did someone knows something about that.
Thanks.
laurent
From: Dhruba <dhrubajhaldar(at)yahoo.com> [ date: 2004-08-31 ]
I am facing problem in mysql_init().
Below is my program.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include <mysql/mysql_version.h>

#include "smpp_util.h"
#include "smpp_log.h"

using namespace std;

MYSQL *smppDbConn;
MYSQL_RES *smscDbRes;
MYSQL_ROW smppDbRow;

int mysqlOpen(param *cfg)
{


char user[20];
char pass[20];
char host[20];
char dbname[20];

strcpy(user,"smsc");
strcpy(pass,"smsc");
strcpy(host,"10.0.0.236");
strcpy(dbname,"smsc");

free((void *)smppDbConn);
smppDbConn = malloc(sizeof(*smppDbConn));

/* Database initialisation */
info(2,"Address 0x%x",&smppDbConn);
// smppDbConn = mysql_init(NULL);
mysql_init(smppDbConn);
info(2,"After allocation");

/* Connect to database */
if (!mysql_real_connect(smppDbConn,host,user,pass,dbname,0, NULL, 0))
{
error(2,"Could Not Connect To Database \"%s\".mysql_error:\" %s\""
,dbname,mysql_error(smppDbConn));
return FAILURE;
}

return SUCCESS;
}

I am compiling the program using gcc-3.2.3 and mysql-4.0.13.
My program gives segmentation fault after mysql_init
something like this
(gdb) b mysqlOpen
Breakpoint 1 at 0x804ac11: file src/smpp_mysql.c, line 22.
(gdb) r
Starting program: /home/dhruba/prog/smppsim
2004-08-31 12:44:18 [0] INFO: SMPP SMSC SERVER PROGRAM.
2004-08-31 07:14:18 [0] INFO: Configuration File : smppsim.conf [OK].
2004-08-31 07:14:18 [2] INFO: Openend Log File /home/dhruba/prog/log/smpp.log.

Breakpoint 1, mysqlOpen (cfg=0x8073988) at src/smpp_mysql.c:22
22 strcpy(user,"smsc");
(gdb) until 27
mysqlOpen (cfg=0x8073988) at src/smpp_mysql.c:28
28 info(2,"Address 0x%x",&smppDbConn);
(gdb) n
2004-08-31 07:14:24 [2] INFO: Address 0xbfffe53c.
29 smppDbConn = mysql_init(NULL);
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0xb74c8c0b in _int_malloc () from /lib/tls/libc.so.6
(gdb) q
The program is running. Exit anyway? (y or n) y

Can any one please help me out???
From: laurent [ date: 2004-09-02 ]
Hello Dhruba,
You should try mysql_init(NULL) the pointer need to be initialize I think this is where is your problem.
Bye

From: wuzhuang <wuzhuang_0001(at)163.com> [ date: 2005-05-11 ]
Thank you very much!!
It's really a charming article!!

From: Baudot Guillaume <guillaume.baudot(at)caramail.com> [ date: 2005-05-12 ]
from:Emmanuel Delahaye

>Code errone / Wrong code
>[...]
>
>the following code is buggy:
> char *query;
> <...>
> sprintf(query,"select * from %s\n", MY_TABLE_NAME);
>
>
>the 'query' pointer has never been initialized, and an undefined >behaviour is invoked.

Emmanuel suggested me to use a static array, or dynamic allocation instead of an uninitialized pointer, and he's perfectly right !
THEN DO "char *query[N];" OR "char *query=malloc(N*sizeof(char));"
with N=512, it should be sufficient...

PS code is wrong in both original example and my version !
From: sunset <sunset.2(at)email.com> [ date: 2005-05-17 ]
I would like to thank the author for a good article.
His code works for me, while lots of other one won't.
Sorry to everybody, for whom this code doesn't compile.
Probably some other code will work for you. Good luck.

As for C APIs I would expect something more natural.
It seems a bit odd for me to construct an SQL operator
from my data, and making the server parse it out.
From: Anonymous User [ date: 2006-01-09 ]
the data that is retrieved from the tables is a string despite it being declared as a float and a decimal.i have problems altering this to integer and float.also whenever i try to retrieve data using a float value,it just returns garbage values.

can anyone help me on this??
From: mzero83 <mzero83(at)hotmail.com> [ date: 2006-04-04 ]
i had problems with the tcp ip socket creation with the folowing warning:Can't create TCP/IP socket (24). if anyone has the same problem try this:

char sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
mysql = mysql_init(NULL);
if (!mysql_real_connect(mysql,"host","user",
"pass","db_name",atoi("3306"),&sock,0))
{
printf( "Error connecting to database: %s\n",mysql_error(mysql));
}
else{
printf("Connected...\n");
}

From: Benny <benvarughes(at)yahoo.co.in> [ date: 2006-04-10 ]
Our program need to keep the mysql handle as a global variable.
So, before executing a query we have to make sure that connection
exists or not.

That means




How to check the mysql connection is timedout or not?
From: Nikhil Mahabudhe <nikhil.mahabudhe(at)gmail.com> [ date: 2006-06-23 ]
In my program i was able to make connection but "mysql_query" is failing, and mysql_errno returns '0'.

mysql_real_connect(&mysql,"localhost","root","Pending",0,NULL,0) -- in this 7 parameters(This is Successful) , but if i used 8 parameters then
it gives error "too many arguments to function mysql_real_connect".
mysql_query(&mysql,"SELECT name from user limit 3") this is what i used in my program.
i am compiling program with following command--
"gcc -o ex1 ex1.c -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient -lz"
My MYSQL_SERVER_VERSION -3.21.33b,MYSQL_VERSION_ID-32133
Operating System-Solaris
i will be really thankful to you for helping Me.

From: Nikhil Mahabudhe <nikhil.mahabudhe(at)gmail.com> [ date: 2006-06-23 ]
In my program i was able to make connection but "mysql_query" is failing, and mysql_errno returns '0'.

mysql_real_connect(&mysql,"localhost","root","Pending",0,NULL,0) -- in this 7 parameters(This is Successful) , but if i used 8 parameters then
it gives error "too many arguments to function mysql_real_connect".
mysql_query(&mysql,"SELECT name from user limit 3") this is what i used in my program.
i am compiling program with following command--
"gcc -o ex1 ex1.c -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient -lz"
My MYSQL_SERVER_VERSION -3.21.33b,MYSQL_VERSION_ID-32133
Operating System-Solaris
i will be really thankful to you for helping Me.

20 talkbacks




Due to the increased amount of web spam we have deciced to removed the talkback posting possibility. You can read old talkbacks but you can no longer post new ones.

Back to http://cgi.linuxfocus.org/English/September2003/article304.shtml

Please contact webmaster(at)linuxfocus.org if you have any questions with regards to this talkback

lftalkback version 3.10