Segmentation fault problem with mysql_query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aerobat
    New Member
    • Mar 2008
    • 3

    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:

    [code=c]
    #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 airportslocatio n WHERE icao = \"%s\"",airport );

    conn = mysql_init(NULL );

    /* Connect to database */
    if (!mysql_real_co nnect(conn, server,
    user, password, database, 0, NULL, 0)) {
    fprintf(stderr, "%s\n", mysql_error(con n));
    exit(1);
    }

    /* send SQL query */

    printf(query);

    if (mysql_query(co nn, query)) {
    fprintf(stderr, "%s\n", mysql_error(con n));
    exit(1);
    } while ((row = mysql_fetch_row (res)) != NULL)
    printf("%s \n", row[0]);
    /* close connection */
    mysql_free_resu lt(res);
    mysql_close(con n);
    return 0;
    }
    [/code]

    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
    Last edited by sicarie; Mar 22 '08, 06:20 PM. Reason: Friggin code tags
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #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?

    Comment

    • aerobat
      New Member
      • Mar 2008
      • 3

      #3
      Originally posted by sicarie
      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)....

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by aerobat
        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.

        Comment

        • aerobat
          New Member
          • Mar 2008
          • 3

          #5
          Originally posted by sicarie
          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...

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by aerobat
            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...

            Comment

            • pongscript
              New Member
              • Mar 2007
              • 27

              #7
              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.

              Comment

              Working...