|
Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com |
From: Ravi raj (raviraj
vinjey.com)
Date: Fri May 08 2009 - 05:04:02 CDT
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 <stdio.h>
#include <stdlib.h>
#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" <wharms
bfs.de>
To: "Ravi raj" <raviraj
vinjey.com>
Cc: <mysql
lists.mysql.com>; "Vinoth Kumar" <vinoth
vinjey.com>
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 <stdio.h>
> #include <stdlib.h>
> #include <mysql/mysql.h>
>
> 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 <stdio.h>
>> 2.. #include <stdlib.h>
>> 3.. #include <string.h>
>> 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
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]