Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

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

  1. #11
    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
    In MySQL, MySQLdb.connect() returns a Connection object that has an excape_string() method that does the escaping for you.

    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.
    in pysqlite library there is no escaping method in Connection class. And that codeline doesn't work either. Believe me, I have tested all possible and impossible lines. But you are right, I should do this with library functions...
    Code:
    >>> s = "It's nice to have an example"
    >>> s.replace("'", "\\'")
    "It\\'s nice to have an example"
    >>> s.replace("'", "\'")
    "It's nice to have an example"
    >>> s.replace("'", "\\\'")
    "It\\'s nice to have an example"
    Since the sql and parameters are now separate it's possible for the library to take care of escaping.
    Ok thanks, I have to test this one.

  2. #12
    Join Date
    Feb 2010
    Beans
    1

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

    Thanks for posting this. It really helped me solved my headaches. Just so simple. How I love Python. Thanks again.

Page 2 of 2 FirstFirst 12

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
  •