Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: [PYTHON] Frustrated with string escaping, please help!

  1. #1
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    300
    Distro
    Ubuntu 7.04 Feisty Fawn

    [PYTHON] Frustrated with string escaping, please help!

    Ok, I've been fighting with this for a while now I find it impossible to add string with ' to SQLite database.

    I have a simple code that uses pysqlite:
    Code:
    db_conn = sqlite.connect("test.db")
    db_cur = db_conn.cursor()
    db_cur.execute("UPDATE test_table SET field_1='%s' WHERE field_2='%s'" % (data, condition))
    db_conn.commit()
    db_conn.close()
    Now, if data is a string:
    Code:
    It's nice to program with python!
    It fails to update it. It seems to be, because of ' char. Without that everything works. Now I've tried to escape that char away with
    Code:
    data.replace('\'','\\\'')
    But it stll fails...

    Any ideas? Is there a better way to do escaping for SQL strings?

  2. #2
    Join Date
    Oct 2006
    Beans
    10

    Re: [PYTHON] Frustrated with string escaping, please help!

    Can you triple quote it?

    Usually if I have a string with multiple quotes or apostrophes in it I will triple quote it, like:

    '''This is a ' test ' of triple apostrophes'''

    """ this is a "test" of triple quotes"""

    When python sees a triple quote it continues reading the string until it hits another triple quote. This may work.

  3. #3
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    300
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [PYTHON] Frustrated with string escaping, please help!

    Quote Originally Posted by slightcrazed View Post
    Can you triple quote it?
    Thanks for reply,

    I think I can't because I get this string as a return value of library method call.

  4. #4
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: [PYTHON] Frustrated with string escaping, please help!

    library returns a string which contains single quotes?

    Can it contain also double quotes? or never?

  5. #5
    Join Date
    Oct 2006
    Beans
    10

    Re: [PYTHON] Frustrated with string escaping, please help!

    Quote Originally Posted by Laterix View Post
    Thanks for reply,

    I think I can't because I get this string as a return value of library method call.

    Actually, now that I look at the issue again, I think you need 2 modifications. It's actually the SQL that's getting messed up, and not python. Have you tried:


    db_cur.execute('''UPDATE test_table SET field_1="%s" WHERE field_2="%s"''' % (data, condition))

    I 'triple apostrophe' the UPDATE statement and then use quotes for the %s. This way, when you pass it a single quote it won't prematurely end the statement.

    Worth a shot.

  6. #6
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    300
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [PYTHON] Frustrated with string escaping, please help!

    Quote Originally Posted by slightcrazed View Post
    I 'triple apostrophe' the UPDATE statement and then use quotes for the %s. This way, when you pass it a single quote it won't prematurely end the statement.
    Thank you! This seems to work just fine... Excellent!
    Last edited by Laterix; June 15th, 2007 at 04:29 PM.

  7. #7
    Join Date
    Jun 2005
    Beans
    2,314

    Re: [PYTHON] Frustrated with string escaping, please help!

    What you shouodl be doing is parsing the input string and escaping that. If you did so, the "It's" would be converted to "it\'s" which causes the SQL interpreter to correctly treat the \' as a single quote and not as the end of the SQL statement. You could of course do this by doing:
    inputString.replace("'", "\'")
    but there are other characters that should be escaped too, and I can't remember which now. The database library should provide you with an escape function that does this input string preparation for you.

  8. #8
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    300
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [PYTHON] Frustrated with string escaping, please help!

    Quote Originally Posted by steve.horsley View Post
    What you shouodl be doing is parsing the input string and escaping that. If you did so, the "It's" would be converted to "it\'s" which causes the SQL interpreter to correctly treat the \' as a single quote and not as the end of the SQL statement. You could of course do this by doing:
    inputString.replace("'", "\'")
    I'm well aware of that. But the problem was how to do it, because the line you gave doesn't work. That was the first thing that I tried.

    but there are other characters that should be escaped too, and I can't remember which now. The database library should provide you with an escape function that does this input string preparation for you.
    I'm in impression that pysqlite includes class Cursor whitch execute() method should do this kind of escapeing automatically when I query database. But for some reason, it didn't work for me. Maybe I missunderstood something there.

  9. #9
    Join Date
    Jun 2005
    Beans
    2,314

    Re: [PYTHON] Frustrated with string escaping, please help!

    In MySQL, MySQLdb.connect() returns a Connection object that has an excape_string() method that does the escaping for you. It seems that Cursor.execute(string) expects the string to be already escaped. My geuss is that they all work the same way but I'd have to check the python database docs to be sure.

    And I just realised that you need to esacpe the backspace in the command I gave you, so it should be:
    inputString.replace("'", "\\'")
    but of course you really should use the library function.

  10. #10
    Join Date
    Dec 2006
    Location
    Uk
    Beans
    109

    Re: [PYTHON] Frustrated with string escaping, please help!

    Quote Originally Posted by Laterix View Post
    I'm in impression that pysqlite includes class Cursor whitch execute() method should do this kind of escapeing automatically when I query database. But for some reason, it didn't work for me. Maybe I missunderstood something there.
    Code:
    db_cur.execute("UPDATE test_table SET field_1='%s' WHERE field_2='%s'" % (data, condition))
    It can't escape automatically because you are just passing it a query string. If you want it to escape automatically you need to pass it a parameterized SQL statement and the parameters.

    Something like this.

    Code:
    db_cur.execute("UPDATE test_table SET field_1=? WHERE field_2=?", data, condition)
    Since the sql and parameters are now separate it's possible for the library to take care of escaping.
    Last edited by winch; June 18th, 2007 at 12:42 PM.
    OpenStreetMap - Free editable map of the whole world

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •