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
To unsubscribe: http://lists.mysql.com/[email protected]