473,808 Members | 2,761 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segmentation fault problem with mysql_query

3 New Member
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 airportslocatio n WHERE icao = "find"

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

mysql_query(con n, "SELECT lat,longitude FROM airportslocatio n 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 airportslocatio n WHERE icao = \"%s\"",airport );

than:

mysql_query(con n, query)

also i tried:

sprintf(query," SELECT lat,longitude FROM airportslocatio n 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 10989
sicarie
4,677 Recognized Expert Moderator Specialist
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
aerobat
3 New Member
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 airportslocatio n WHERE icao ="find"

thanks for the quick reply.....

also if i just send

SELECT lat,longitude FROM airportslocatio n 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 Recognized Expert Moderator Specialist
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 airportslocatio n WHERE icao ="find"

thanks for the quick reply.....

also if i just send

SELECT lat,longitude FROM airportslocatio n 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
aerobat
3 New Member
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 Recognized Expert Moderator Specialist
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
pongscript
27 New Member
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',"fin d");

then:
mysql_query(mys qlconnection,Qu ery);

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
1797
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 of a call to mysql_query() function mergeProfiles($res) {
6
8430
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 line and it threw a Segmentation Fault. Googling produces a ton of information on this subject but al of specific to certain binaries and it doesn't seem germaine to solving my problem. Any one have any advice? Whole script is below.
3
11452
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 () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
5
2999
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 understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
2
744
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 just fine but when it comes to the 2nd ive got an error: segmentation fault. my query is fine tried it out directly.
18
26125
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)? Thanks.
27
3375
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! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
7
5883
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 testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
3
5188
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 from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.