MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old November 1st, 2004, 02:49 AM
hwangc hwangc is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 2 hwangc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
MySQL and C

This might be a C problem.
I'm using gcc(3.2) under Linux.
The compilation is OK but execution cause Segmentation fault.
The error was caused in the line:
temp_rate->point = atoi(row[1]); in function CheckRate()
Please help. TIA.

The source code are follow:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mysql.h>

struct rate
{
char number[64];
char prefix[32];
int id;
int point;
int second;
int duration;
};

int main(int argc, char *argv[])
{

if (argc < 2)
{
exit(0);
}

struct rate current_rate;
strcpy(current_rate.number, argv[1]);
current_rate.id = 1;

MYSQL *conn;

conn = mysql_init(NULL);
if (conn == NULL)
{
fprintf(stderr, "mysql_init() failed (probably out of memory)\n");
exit(1);
}

if (mysql_options(conn, MYSQL_READ_DEFAULT_FILE, "./config.ini") != 0)
fprintf(stderr, "loding config file error\n");

if (mysql_real_connect(conn, NULL, NULL, NULL, NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "mysql_real_connect() failed:\nError %u (%s)\n",
mysql_errno(conn), mysql_error(conn));
mysql_close(conn);
exit(1);
}

if (CheckRate(conn, &current_rate))
{
printf("Found\n");
printf("Code = %s,\tRate = %s,\tNumber = %s,\tSECS = %d,\tLIMITS = %d\n", cu
rrent_rate.prefix, current_rate.point, current_rate.number, current_rate.second,
current_rate.duration);
}
else
{
printf("Not Found\n");
}

mysql_close(conn);
exit(0);
}

int CheckRate(MYSQL *conn, struct rate *temp_rate)
{
MYSQL_RES *res_set;
int FOUND = 0;
char sql_cmd[128];
strcpy(temp_rate->prefix, "NOT FOUND");

sprintf(sql_cmd, "SELECT CODE, RATE, SECS, LIMITS FROM rate WHERE GID='%d' ORD
ER BY LENGTH(CODE) DESC", temp_rate->id);
//printf("%s\n", sql_cmd);
if (mysql_query(conn, sql_cmd) != 0)
{
fprintf(stderr, "query error\n");
}
else
{

res_set = mysql_store_result(conn);
}

MYSQL_ROW row;
while ((row = mysql_fetch_row(res_set)) != NULL)
{
if (strncmp(row[0], temp_rate->number, strlen(row[0])) == 0)
{
strcpy(temp_rate->prefix, row[0]);
temp_rate->point = atoi(row[1]);
temp_rate->second = atoi(row[2]);
temp_rate->duration = atoi(row[3]);

FOUND = 1;
break;
}
}

printf("Code = %s,\tRate = %s,\tNumber = %s,\tSECS = %d,\tLIMITS = %d\n", temp
_rate->prefix, temp_rate->point, temp_rate->number, temp_rate->second, temp_rate
->duration);
mysql_free_result(res_set);
return (FOUND);
}

Reply With Quote
  #2  
Old November 1st, 2004, 08:44 PM
hwangc hwangc is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 2 hwangc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Got it to work.
There're some hidden characters in the file. I re-typed the program again and everything is fine.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > MySQL and C


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway