473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Segmentation fault problem with mysql_query

hello all just wondering if anyone can help me with this problem. Ii has to do with a simple mysql_query. i have tried so many different ways of doing it, but it wont work. here is the code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <mysql.h>
  5.  
  6. main() {
  7.  
  8.    MYSQL *conn;
  9.    MYSQL_RES *res;
  10.    MYSQL_ROW row;
  11.  
  12.    char *server = "localhost";
  13.    char *user = "root";
  14.    char *password = "********"; /* set me first */
  15.    char *database = "test";
  16.    char airport[5] = "find";
  17.  
  18.    char query[100];
  19.  
  20.     sprintf(query,"SELECT lat,longitude FROM airportslocation WHERE icao = \"%s\"",airport);
  21.  
  22.    conn = mysql_init(NULL);
  23.  
  24.    /* Connect to database */
  25.    if (!mysql_real_connect(conn, server,
  26.          user, password, database, 0, NULL, 0)) {
  27.       fprintf(stderr, "%s\n", mysql_error(conn));
  28.       exit(1);
  29.    }
  30.  
  31.    /* send SQL query */
  32.  
  33.     printf(query);
  34.  
  35. if (mysql_query(conn, query)) {
  36.       fprintf(stderr, "%s\n", mysql_error(conn));
  37.       exit(1);
  38.    }    while ((row = mysql_fetch_row(res)) != NULL)
  39.       printf("%s \n", row[0]);
  40. /* close connection */
  41.    mysql_free_result(res);
  42.    mysql_close(conn);
  43.     return 0;
  44. }
  45.  
Ok no here is the probelm, where i set up the query string i use \" if i remove those it will run, but it will think test is a colum in my database it isnt. i need to put " around it so the sql statement is:

SELECT lat,longitude FROM airportslocation WHERE icao = "find"

No if i run it directly it runs fine, like this:

mysql_query(conn, "SELECT lat,longitude FROM airportslocation WHERE icao = \"find\"")

it is when i make a string with the query it screws up as you see in my code above, or listed here:

sprintf(query,"SELECT lat,longitude FROM airportslocation WHERE icao = \"%s\"",airport);

than:

mysql_query(conn, query)

also i tried:

sprintf(query,"SELECT lat,longitude FROM airportslocation WHERE icao = '%s'",airport);

and that fails as well.

I am running fedorecore 6 and compiling with gcc. any ideas, i am completly stuck.

Shawn F
Mar 22 '08 #1
6 10932
sicarie
4,677 Expert Mod 4TB
Did you ever print out query after the sprintf() statement to see what the string looked like after you stuck it in there?

Also, what error are you getting?
Mar 22 '08 #2
Did you ever print out query after the sprintf() statement to see what the string looked like after you stuck it in there?

Also, what error are you getting?
I get a segmentation fault then the program aborts. only when i add the \" or '.

I did print out the string query and it contains the correct data which is:

SELECT lat,longitude FROM airportslocation WHERE icao ="find"

thanks for the quick reply.....

also if i just send

SELECT lat,longitude FROM airportslocation WHERE icao =find

it will get a find colume dosent exist, which is fine since i am looking for find in the colume icao.

so i need the quotes for the call to work correct. Funny thing is I do it with ' in perl and it is fine (i am actually converted parts of a old program)....
Mar 22 '08 #3
sicarie
4,677 Expert Mod 4TB
I get a segmentation fault then the program aborts. only when i add the \" or '.

I did print out the string query and it contains the correct data which is:

SELECT lat,longitude FROM airportslocation WHERE icao ="find"

thanks for the quick reply.....

also if i just send

SELECT lat,longitude FROM airportslocation WHERE icao =find

it will get a find colume dosent exist, which is fine since i am looking for find in the colume icao.

so i need the quotes for the call to work correct. Funny thing is I do it with ' in perl and it is fine (i am actually converted parts of a old program)....
Hmmm. I'm on my work laptop (WinXP, sadly), and just lost my Debian VM, so I can't test a mysql connection, but I'd recommend trying (in lieu of other help) doubly escaping the quotes - \\"%s\\" possibly.

Another idea is to use the apostrophe, though that will probably need to be escaped as well.

Hopefully someone will be able to help you, if not, I am trying to have my linux box back up by tonight. So hopefully (hopefully) I will be able to.
Mar 22 '08 #4
Hmmm. I'm on my work laptop (WinXP, sadly), and just lost my Debian VM, so I can't test a mysql connection, but I'd recommend trying (in lieu of other help) doubly escaping the quotes - \\"%s\\" possibly.

Another idea is to use the apostrophe, though that will probably need to be escaped as well.

Hopefully someone will be able to help you, if not, I am trying to have my linux box back up by tonight. So hopefully (hopefully) I will be able to.
Yah i tried \' same problem. it has me stumped. It should work, but doesnt. Especially since when i do it direct it works. really wierd. i also tried \\"%s\\" and that gives errors on compile...
Mar 22 '08 #5
sicarie
4,677 Expert Mod 4TB
Yah i tried \' same problem. it has me stumped. It should work, but doesnt. Especially since when i do it direct it works. really wierd. i also tried \\"%s\\" and that gives errors on compile...
Well, I'm not surprised that it doesn't work when not direct (I did a bunch of Java SQL stuff recently, and it was the same way), it's just probably one little quirk of the mysql connection that we're missing...
Mar 22 '08 #6
actual this wont work

SELECT * FROM field="SAMPLE"

but using quote instead of double quote will fix this.


SELECT * FROM field='Sample'


on c++ program try these:

char Query[160];

sprintf(Query,"SELECT * From field='%s',"find");

then:
mysql_query(mysqlconnection,Query);

just replace mysqlconnection with your connection object;

by the way the error was not on the program but on mysql parsing stuff on the server.. if it can't recognize the query. it will not return a resultset .. so when you try to access a empty result set or a NULL result set it will cause an error since you can't read when nothings there.
Apr 4 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sébastien | last post by:
Hi there, If I call mergeProfiles() only one time, it is fine. However, if I call it more than one time (say twice), then it will returns a segmentation fault... any ideas? $res is a result...
6
by: jerrygarciuh | last post by:
Hello, I have a script running in the wee hours via cron job. When I access the script via browser it works like a charm. I noticed it was not succeeding and ran it manually from the command...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
2
by: Chris Grossbe | last post by:
Hi! this problem has been a headache for about 2 hours now and i just cant figure it out. ive got a c program that connects to a DB via mysql. currently ive got 2 querys. the first worked...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.