Memory corrupting, while retrive the query generated

Memory corrupting, while retrive the query generated

am 07.05.2009 10:58:00 von Ravi raj

------=_NextPart_000_0030_01C9CF20.05D5A380
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Dear All,

I want to connect MYSQL with following C application , while =
i'm trying to retrive the query generated , its corrupting the memory.=20

Is there any solution , to retrive the query generated with out any =
memory crashes?

Please help me to solve this problem.

code as follows,

------------------------------------------------------------ -------------=
------------------------------------------------------------ -------------=
-----------

1.. #include
2.. #include
3.. #include
4.. #include "mysql.h"
5.. ?
6.. int main()
7.. {
8.. MYSQL *conn;
9.. MYSQL_RES *res;
10.. MYSQL_ROW row;
11.. MYSQL_FIELD *field;
12.. unsigned int i =3D 0;
13.. char table_type[30];
14.. char buffer[200];
15.. unsigned int num_fields;
16.. char *server =3D "localhost";
17.. char *user =3D "root";
18.. char *password =3D ""; /* set me first */
19.. char *database =3D "test";
20.. conn =3D mysql_init(NULL);
21.. ?
22.. /* Connect to database */
23.. if (!mysql_real_connect(conn, server, user, password, database, =
0, NULL, 0))
24.. {
25.. fprintf(stderr, "%s\n", mysql_error(conn));
26.. exit(1);
27.. }
28.. ?
29.. if(mysql_ping(conn))
30.. {
31.. printf("error in connection \n");
32.. exit(1);
33.. }
34.. sprintf(table_type, "method");
35.. ?
36.. sprintf(buffer, "select mid, mname from %s;", table_type);
37.. mysql_query(conn, buffer);
38.. res =3D mysql_store_result(conn);
39.. num_fields =3D mysql_num_fields(res);
40..=20
41.. while ((row =3D mysql_fetch_row(res)) !=3D NULL)
42.. {
43.. for(i =3D 0;i < num_fields;i++) =
//here is the problem , num_fields is corrupting=20
44.. printf("%s\n", row[i]?row[i]:"NULL");
45.. }
46.. mysql_free_result(res);
47.. mysql_close(conn);
48.. return 0;
49.. }
------------------------------------------------------------ -------------=
------------------------------------------------------------ -------------=
---------------


Regards,=20
Raviraj
-----------------------------------------
mobile : (91) (0) 9742293013
www.vinjey.com
P Think before you print
/* work should be challenging=20
and the challenge should be fun */
------=_NextPart_000_0030_01C9CF20.05D5A380--

Re: Memory corrupting, while retrive the query generated

am 07.05.2009 15:10:08 von walter harms

hi ravi,

this works for me. it should help
you to get a starting point



re,
wh


/*
simpple DB connect test
gcc -L/usr/lib/mysql -lmysqlclient connect.c
*/

#define _GNU_SOURCE
#include
#include
#include

int main()
{
MYSQL *MySQL;
MYSQL_ROW row;
MYSQL_RES *res;
char *dbhost = "localhost";
char *dbuser = "dbuser";
char *dbpass = "";
char *dbname = "mysql";
char *sel_smt;
int ret;

MySQL = mysql_init(NULL);
if (MySQL == NULL) {
fprintf(stderr, "Connection failed\n");
exit(1);
}


if (mysql_real_connect
(MySQL, dbhost, dbuser, dbpass, dbname, 0, NULL, 0) < 0) {

fprintf(stderr, "%s\n", mysql_error(MySQL));
exit(1);
}


asprintf(&sel_smt, "select count(*) from user");


if (mysql_query(MySQL, sel_smt) != 0) {

fprintf(stderr, "%s\n", mysql_error(MySQL));
exit(1);
}


res = mysql_store_result(MySQL);
if (res == NULL) {

fprintf(stderr, "%s\n", mysql_error(MySQL));
exit(1);
}

row = mysql_fetch_row(res);

printf("%s\n", row[0] ? row[0] : "NULL");

free(sel_smt);
mysql_free_result(res);

mysql_close(MySQL);
exit(0);
}



Ravi raj schrieb:
> Dear All,
>
> I want to connect MYSQL with following C application , while i'm trying to retrive the query generated , its corrupting the memory.
>
> Is there any solution , to retrive the query generated with out any memory crashes?
>
> Please help me to solve this problem.
>
> code as follows,
>
> ------------------------------------------------------------ ------------------------------------------------------------ -------------------------------------
>
> 1.. #include
> 2.. #include
> 3.. #include
> 4.. #include "mysql.h"
> 5.. ?
> 6.. int main()
> 7.. {
> 8.. MYSQL *conn;
> 9.. MYSQL_RES *res;
> 10.. MYSQL_ROW row;
> 11.. MYSQL_FIELD *field;
> 12.. unsigned int i = 0;
> 13.. char table_type[30];
> 14.. char buffer[200];
> 15.. unsigned int num_fields;
> 16.. char *server = "localhost";
> 17.. char *user = "root";
> 18.. char *password = ""; /* set me first */
> 19.. char *database = "test";
> 20.. conn = mysql_init(NULL);
> 21.. ?
> 22.. /* Connect to database */
> 23.. if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
> 24.. {
> 25.. fprintf(stderr, "%s\n", mysql_error(conn));
> 26.. exit(1);
> 27.. }
> 28.. ?
> 29.. if(mysql_ping(conn))
> 30.. {
> 31.. printf("error in connection \n");
> 32.. exit(1);
> 33.. }
> 34.. sprintf(table_type, "method");
> 35.. ?
> 36.. sprintf(buffer, "select mid, mname from %s;", table_type);
> 37.. mysql_query(conn, buffer);
> 38.. res = mysql_store_result(conn);
> 39.. num_fields = mysql_num_fields(res);
> 40..
> 41.. while ((row = mysql_fetch_row(res)) != NULL)
> 42.. {
> 43.. for(i = 0;i < num_fields;i++) //here is the problem , num_fields is corrupting
> 44.. printf("%s\n", row[i]?row[i]:"NULL");
> 45.. }
> 46.. mysql_free_result(res);
> 47.. mysql_close(conn);
> 48.. return 0;
> 49.. }
> ------------------------------------------------------------ ------------------------------------------------------------ -----------------------------------------
>
>
> Regards,
> Raviraj
> -----------------------------------------
> mobile : (91) (0) 9742293013
> www.vinjey.com
> P Think before you print
> /* work should be challenging
> and the challenge should be fun */

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org

Re: Memory corrupting, while retrive the query generated

am 08.05.2009 12:04:02 von Ravi raj

Dear walter Harms,

Thanks for your valuable solution, but in the code which you
provided is printing only one row , if i try to print whole table, or 2, or
3, columns fully means its giving segmentation fault, kindly check the below
code for furthur information.

software used:
---------------
1. MYSQL 6.0.0
2.MySQL Connection C 6.0
3.Cygwin (used to run the programs using GCC)

Operating Systems:
--------------------
Windows Vista Home basic

building executable:
--------------------------
exporting c connection library (mysql.h) as,
export PATH=$PATH:"c:/Program Files/MySQL/MySQL Connection C 6.0/lib/opt"

and i copied the " libmysql.dll " to local folder where the c code
resides,


gcc -g -c simple.c
gcc -g libmysql.dll simple.o

running:
------------
../a.exe



if i run the below code its giving output like this ,(trying to get all
values from a particular column of a table).

---------------------------------------####----output-----## ##-------------------------------------------------------
num_fields = 2
127.0.0.1

localhost
28 [main] a 3836 _cygtls::handle_exceptions: Error while dumping state
(probably corrupted stack)
Segmentation fault (core dumped)
------------------------------------------------------------ ------------------------------------------------------------

Code as follows: (i just modified your code , so as to print all the values
regarding particular column)

------------------------------------------------------------ --------------------------------------------------code
starts here
/*
simple DB connect test
gcc -L/usr/lib/mysql -lmysqlclient connect.c
*/

#define _GNU_SOURCE
#include
#include
#include "C:\Program Files\MySQL\MySQL Connector C 6.0.0\include\mysql.h"

int main()
{
MYSQL *MySQL;
MYSQL_ROW row;
MYSQL_RES *res;
char *dbhost = "localhost";
char *dbuser = "root";
char *dbpass = "";
char *dbname = "mysql";
char sel_smt[200];
int ret;

unsigned int num_fields, i;

MySQL = mysql_init(NULL);
if (MySQL == NULL) {
fprintf(stderr, "Connection failed\n");
exit(1);
}


if (mysql_real_connect
(MySQL, dbhost, dbuser, dbpass, dbname, 0, NULL, 0) < 0) {

fprintf(stderr, "%s\n", mysql_error(MySQL));
exit(1);
}


//sprintf(sel_smt, "select * from user;");

//printf("\n %s\n", sel_smt);


if (mysql_query(MySQL, "select host, user from user;") != 0) {

fprintf(stderr, "%s\n", mysql_error(MySQL));
exit(1);
}


res = mysql_store_result(MySQL);
if (res == NULL) {

fprintf(stderr, "%s\n", mysql_error(MySQL));
exit(1);
}

//row = mysql_fetch_row(res);

//printf("%s\n", row[0] ? row[0] :
--------------------------------------> instead of printing one row

num_fields = mysql_num_fields(res);
|

|
printf("\n num_fields = %d\n", num_fields);
|

|
while ((row = mysql_fetch_row(res)) != NULL)
|----------------------> printing host and user column fully
{
|
for(i = 0;i < num_fields;i++)
|
printf("%s\n", row[i]?row[i]:"NULL");
|

|
}
|


//free(sel_smt);
mysql_free_result(res);

mysql_close(MySQL);
exit(0);
}

------------------------------------------------------------ ------------------------------------------------------------ ------code
ends here


Thanks and regards,
Ravi




----- Original Message -----
From: "walter harms"
To: "Ravi raj"
Cc: ; "Vinoth Kumar"
Sent: Thursday, May 07, 2009 6:40 PM
Subject: Re: Memory corrupting, while retrive the query generated


>
> hi ravi,
>
> this works for me. it should help
> you to get a starting point
>
>
>
> re,
> wh
>
>
> /*
> simpple DB connect test
> gcc -L/usr/lib/mysql -lmysqlclient connect.c
> */
>
> #define _GNU_SOURCE
> #include
> #include
> #include
>
> int main()
> {
> MYSQL *MySQL;
> MYSQL_ROW row;
> MYSQL_RES *res;
> char *dbhost = "localhost";
> char *dbuser = "dbuser";
> char *dbpass = "";
> char *dbname = "mysql";
> char *sel_smt;
> int ret;
>
> MySQL = mysql_init(NULL);
> if (MySQL == NULL) {
> fprintf(stderr, "Connection failed\n");
> exit(1);
> }
>
>
> if (mysql_real_connect
> (MySQL, dbhost, dbuser, dbpass, dbname, 0, NULL, 0) < 0) {
>
> fprintf(stderr, "%s\n", mysql_error(MySQL));
> exit(1);
> }
>
>
> asprintf(&sel_smt, "select count(*) from user");
>
>
> if (mysql_query(MySQL, sel_smt) != 0) {
>
> fprintf(stderr, "%s\n", mysql_error(MySQL));
> exit(1);
> }
>
>
> res = mysql_store_result(MySQL);
> if (res == NULL) {
>
> fprintf(stderr, "%s\n", mysql_error(MySQL));
> exit(1);
> }
>
> row = mysql_fetch_row(res);
>
> printf("%s\n", row[0] ? row[0] : "NULL");
>
> free(sel_smt);
> mysql_free_result(res);
>
> mysql_close(MySQL);
> exit(0);
> }
>
>
>
> Ravi raj schrieb:
>> Dear All,
>>
>> I want to connect MYSQL with following C application , while
>> i'm trying to retrive the query generated , its corrupting the memory.
>>
>> Is there any solution , to retrive the query generated with out any
>> memory crashes?
>>
>> Please help me to solve this problem.
>>
>> code as follows,
>>
>> ------------------------------------------------------------ ------------------------------------------------------------ -------------------------------------
>>
>> 1.. #include
>> 2.. #include
>> 3.. #include
>> 4.. #include "mysql.h"
>> 5.. ?
>> 6.. int main()
>> 7.. {
>> 8.. MYSQL *conn;
>> 9.. MYSQL_RES *res;
>> 10.. MYSQL_ROW row;
>> 11.. MYSQL_FIELD *field;
>> 12.. unsigned int i = 0;
>> 13.. char table_type[30];
>> 14.. char buffer[200];
>> 15.. unsigned int num_fields;
>> 16.. char *server = "localhost";
>> 17.. char *user = "root";
>> 18.. char *password = ""; /* set me first */
>> 19.. char *database = "test";
>> 20.. conn = mysql_init(NULL);
>> 21.. ?
>> 22.. /* Connect to database */
>> 23.. if (!mysql_real_connect(conn, server, user, password, database, 0,
>> NULL, 0))
>> 24.. {
>> 25.. fprintf(stderr, "%s\n", mysql_error(conn));
>> 26.. exit(1);
>> 27.. }
>> 28.. ?
>> 29.. if(mysql_ping(conn))
>> 30.. {
>> 31.. printf("error in connection \n");
>> 32.. exit(1);
>> 33.. }
>> 34.. sprintf(table_type, "method");
>> 35.. ?
>> 36.. sprintf(buffer, "select mid, mname from %s;", table_type);
>> 37.. mysql_query(conn, buffer);
>> 38.. res = mysql_store_result(conn);
>> 39.. num_fields = mysql_num_fields(res);
>> 40..
>> 41.. while ((row = mysql_fetch_row(res)) != NULL)
>> 42.. {
>> 43.. for(i = 0;i < num_fields;i++)
>> //here is the problem , num_fields is corrupting
>> 44.. printf("%s\n", row[i]?row[i]:"NULL");
>> 45.. }
>> 46.. mysql_free_result(res);
>> 47.. mysql_close(conn);
>> 48.. return 0;
>> 49.. }
>> ------------------------------------------------------------ ------------------------------------------------------------ -----------------------------------------
>>
>>
>> Regards,
>> Raviraj
>> -----------------------------------------
>> mobile : (91) (0) 9742293013
>> www.vinjey.com
>> P Think before you print
>> /* work should be challenging
>> and the challenge should be fun */
>
>



--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org

Re: Memory corrupting, while retrive the query generated

am 08.05.2009 14:03:28 von walter harms

Ravi raj schrieb:
> Dear walter Harms,
>
> Thanks for your valuable solution, but in the code which
> you provided is printing only one row , if i try to print whole table,
> or 2, or 3, columns fully means its giving segmentation fault, kindly
> check the below code for furthur information.
>
> software used:
> ---------------
> 1. MYSQL 6.0.0
> 2.MySQL Connection C 6.0
> 3.Cygwin (used to run the programs using GCC)
>
> Operating Systems:
> --------------------
> Windows Vista Home basic
>
> building executable:
> --------------------------
> exporting c connection library (mysql.h) as,
> export PATH=$PATH:"c:/Program Files/MySQL/MySQL Connection C 6.0/lib/opt"
>
> and i copied the " libmysql.dll " to local folder where the c code
> resides,
>
>
> gcc -g -c simple.c
> gcc -g libmysql.dll simple.o
>
> running:
> ------------
> ./a.exe
>
>
>
> if i run the below code its giving output like this ,(trying to get all
> values from a particular column of a table).
>
> ---------------------------------------####----output-----## ##-------------------------------------------------------
>
> num_fields = 2
> 127.0.0.1
>
> localhost
> 28 [main] a 3836 _cygtls::handle_exceptions: Error while dumping
> state (probably corrupted stack)
> Segmentation fault (core dumped)
> ------------------------------------------------------------ ------------------------------------------------------------


hi Ravi,
i have checked your programm on my box and it works as expected.
(linux,mysql 5.0)

That leaves only your environment (compiler,libraries,...) as culprit.
The most easy think to do now is to install a linux and give it a try.

re,
wh


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org