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: -
#include <stdio.h>
-
#include <math.h>
-
#include <string.h>
-
#include <mysql.h>
-
-
main() {
-
-
MYSQL *conn;
-
MYSQL_RES *res;
-
MYSQL_ROW row;
-
-
char *server = "localhost";
-
char *user = "root";
-
char *password = "********"; /* set me first */
-
char *database = "test";
-
char airport[5] = "find";
-
-
char query[100];
-
-
sprintf(query,"SELECT lat,longitude FROM airportslocation WHERE icao = \"%s\"",airport);
-
-
conn = mysql_init(NULL);
-
-
/* Connect to database */
-
if (!mysql_real_connect(conn, server,
-
user, password, database, 0, NULL, 0)) {
-
fprintf(stderr, "%s\n", mysql_error(conn));
-
exit(1);
-
}
-
-
/* send SQL query */
-
-
printf(query);
-
-
if (mysql_query(conn, query)) {
-
fprintf(stderr, "%s\n", mysql_error(conn));
-
exit(1);
-
} while ((row = mysql_fetch_row(res)) != NULL)
-
printf("%s \n", row[0]);
-
/* close connection */
-
mysql_free_result(res);
-
mysql_close(conn);
-
return 0;
-
}
-
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
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?
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)....
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.
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...
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...
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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) {
|
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.
|
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
|
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...
|
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.
| |
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.
|
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 {
|
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...
|
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'...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |