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 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
6 10932
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 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)....
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.
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...
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',"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.
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...
|
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...
|
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...
|
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...
|
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...
|
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)?...
|
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!
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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
|
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...
|
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...
| |