|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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, ¤t_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); } |
|
#2
|
|||
|
|||
|
Got it to work.
There're some hidden characters in the file. I re-typed the program again and everything is fine. |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > MySQL and C |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|