NSString escaping single quotes

  • Hi,

    I'm probably doing something stupid here.  I'm trying to pass a
    string to ODBC drivers using the ODBCKit.  The example code uses a
    NSString, but the code bombs when trying to execute the SQL
    statement.  The debugger reveals that the string is constructed with
    a \ in front of the ' characters which contain the string for the SQL
    statement.  I suspect this is causing the SQL parser to hang.

    The string is declared like this:

    NSString *sql = [NSString stringWithFormat:@"select * from t_state
    where state_code = '%@'", code];

    The contents of the string being passed is this:

    select * from t_state where state_code = \'xx\'

    So, how do I get rid of the \ in front of the single quotes?

    Thanks,

    Mike
  • On Mar 19, 2007, at 5:33 AM, Michael Sullivan wrote:

    > ...
    > The debugger reveals that the string is constructed with a \ in
    > front of the ' characters which contain the string for the SQL
    > statement.  I suspect this is causing the SQL parser to hang.
    >
    > The string is declared like this:
    >
    > NSString *sql = [NSString stringWithFormat:@"select * from t_state
    > where state_code = '%@'", code];
    >
    > The contents of the string being passed is this:
    >
    > select * from t_state where state_code = \'xx\'
    >
    > So, how do I get rid of the \ in front of the single quotes?

    Hi Michael,

    The backslash-escaped single quotes only seem to appear inside of the
    Xcode debugger window's variable summary column.  Examining the value
    of "sql" manually in gdb reveals the correct format:

      (gdb) po sql
    select * from t_state where state_code = 'XX'

    and NSLogging the string also leaves out the backslashes.  So, I'd
    say those single quotes are escaped for some reason only in the
    process of Xcode formatting the object for display in the variable view.

    -Sean
  • Hi Sean,

    Thanks, that make sense, but I wasn't sure.  I'll look elsewhere for
    the problem.

    Mike

    On Mar 19, 2007, at 22:40 , Sean Murphy wrote:

    > On Mar 19, 2007, at 5:33 AM, Michael Sullivan wrote:
    >
    >> ...
    >> The debugger reveals that the string is constructed with a \ in
    >> front of the ' characters which contain the string for the SQL
    >> statement.  I suspect this is causing the SQL parser to hang.
    >>
    >> The string is declared like this:
    >>
    >> NSString *sql = [NSString stringWithFormat:@"select * from t_state
    >> where state_code = '%@'", code];
    >>
    >> The contents of the string being passed is this:
    >>
    >> select * from t_state where state_code = \'xx\'
    >>
    >> So, how do I get rid of the \ in front of the single quotes?
    >
    > Hi Michael,
    >
    > The backslash-escaped single quotes only seem to appear inside of
    > the Xcode debugger window's variable summary column.  Examining the
    > value of "sql" manually in gdb reveals the correct format:
    >
    > (gdb) po sql
    > select * from t_state where state_code = 'XX'
    >
    > and NSLogging the string also leaves out the backslashes.  So, I'd
    > say those single quotes are escaped for some reason only in the
    > process of Xcode formatting the object for display in the variable
    > view.
    >
    > -Sean
previous month march 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Go to today