2003-09-11 -+- df -+- njpig@21cn.com -+- 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);
} -+- 61.144.50.46 = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; NetCaptor 7.2.0)
2003-11-10 -+- Brad -+- brad@missioncriticalenteprises.com -+- 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 -+- 151.204.212.11 = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; YComp 5.0.2.4)
2003-11-28 -+- Boris Sokol -+- cgifalco@cgifalcon.com -+- 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 -+- 193.86.108.103 = Mozilla/5.0 (Windows; U; Windows NT 5.0; cs-CZ; rv:1.5) Gecko/20031007
2003-11-28 -+- Kayra Otaner -+- kayraotaner@yahoo.com -+- 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.
-+- 64.157.161.194 = Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624 Netscape/7.1
2003-12-04 -+- bourne -+- bourne@freemail.hu -+- Hi there!
Thanks for writing tutorials.
Just a suggestion:
mysql = mysql_init(NULL);
Your version caused segmentation fault.
Bye.
-+- 62.68.175.201 = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)
2004-01-19 -+- Baudot Guillaume -+- guillaume.baudot@caramail.com -+- @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
-+- 81.167.116.218 = Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031117 Galeon/1.3.11a
2004-04-12 -+- Robert Synnott -+- -+- As to how to put stuff into the DB, here's a tutorial that covers that:
http://www.synnottsoftware.com/tutorials/mysql.html -+- 83.70.66.30 = Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/124 (KHTML, like Gecko) Safari/125.1
2004-04-12 -+- Johnny -+- collinsj@yahoo.com -+- 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
-+- 66.6.80.48 = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 4.0)
2004-05-20 -+- sejaul Haque -+- sejaul@rediffmail.com -+- 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
-+- 12.20.194.4 = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; T312461)
2004-08-17 -+- Laurent LEDRU -+- laurentledru@data-mobiles.com -+- 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 -+- 194.250.124.77 = Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040614 Firefox/0.8 (Firefox/0.8 polymorph)
#2004-08-31 -+- Dhruba -+- dhrubajhaldar@yahoo.com -+- 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??? -+- 203.122.14.115 = Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030922
2004-08-31 -+- Dhruba -+- dhrubajhaldar@yahoo.com -+- 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??? -+- 203.122.14.115 = Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030922
2004-09-02 -+- laurent -+- -+- Hello Dhruba,
You should try mysql_init(NULL) the pointer need to be initialize I think this is where is your problem.
Bye
-+- 194.250.124.77 = Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040614 Firefox/0.8 (Firefox/0.8 polymorph)
#2005-02-17 -+- tanmay sahu -+- sahu_tank@yahoo.co.in -+- how can i ge the library file and the header file for the c Apis for my sql.
How can i connect to mysql on vc++ platform.
I want to connect to mysql from my *.cpp files.what are the settings required and what *.lib file and *.h files are required.
please help me...
Regards...
-+- 203.196.189.99 = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
#2005-03-22 -+- manjeet beniwal -+- msbeniwal@rediffmail.com -+- i tried to connect mysql using c language under linux plateform.
i used k-develop environment
i got sucess to connect & execute INSERT , UPDATE & SELECT queries.
but i require such program which 'REQUIRE INPUT FROM USER'.
PRESENTLY i m entring values directly into sql queries like
"insert intto dbname value (aa,d,df);"
please help me to fulfil my requirements.
thanks
manjeet beniwal
msbeniwal@rediffmail.com
-+- 210.212.93.35 = Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
#2005-03-22 -+- Bernard -+- pounardo@hotmail.com -+- Only one small question... I would to make a api C who connect an mysql database. I don't have the mysql/mysql.h library. How can I have it? I try with libmysql-devel but it doessn't work. I'm working on Linux Debian. There is an other things i need to connect my prog with the database?
Thanks a lot for you help...
Bernard -+- 157.164.136.71 = Mozilla/5.0 (Windows; U; Windows NT 5.1; fr-FR; rv:1.7.5) Gecko/20041108 Firefox/1.0
2005-05-11 -+- wuzhuang -+- wuzhuang_0001@163.com -+- Thank you very much!!
It's really a charming article!!
-+- 211.160.75.108 = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)
2005-05-12 -+- Baudot Guillaume -+- guillaume.baudot@caramail.com -+- 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 ! -+- 82.225.44.162 = Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.7) Gecko/20050426 Galeon/1.3.20
2005-05-17 -+- sunset -+- sunset.2@email.com -+- 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. -+- 67.82.190.67 = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
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?? -+- 203.199.133.137 = Mozilla/5.0 (compatible; Konqueror/3.3; Linux) (KHTML, like Gecko)
2006-04-04 -+- mzero83 -+- mzero83@hotmail.com -+- 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");
}
-+- 83.98.234.120 = Mozilla/5.0 (X11; U; Linux i686; nl-NL; rv:1.7.12) Gecko/20051010 Firefox/1.0.7 (Ubuntu package 1.0.7)
2006-04-10 -+- Benny -+- benvarughes@yahoo.co.in -+- 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? -+- 202.68.143.226 = Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.7) Gecko/20050416 Fedora/1.0.3-1.3.1 Firefox/1.0.3
2006-06-23 -+- Nikhil Mahabudhe -+- nikhil.mahabudhe@gmail.com -+- 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.
-+- 203.124.131.68 = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
2006-06-23 -+- Nikhil Mahabudhe -+- nikhil.mahabudhe@gmail.com -+- 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.
-+- 203.124.131.68 = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)