tags:

views:

39

answers:

2

I am recieving errors like this one:

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't Stop.mp3' LIMIT 1' at line 1")

Because I am trying to compare a URL that exists in my DB to one in a variable before I choose to insert it or not with the below code:

`#see if any links in the DB match the crawled link

check_exists_sql = "SELECT * FROM LINKS WHERE link = '%s' LIMIT 1" % item['link'].encode("utf-8") 

cursor.execute(check_exists_sql)`

Obviously the ' character and perhaps other characters are causing problems.

How do I format these URLs to avoid this?

A: 

You need to escape any strings in your MySQL when you place these strings into an SQL statement.

Escaping will, among other things, make sure that apostrophes don't break your SQL and introduce security vulnerabilities.

Use mysql.escape_string() (where mysql is your database object)

thomasrutter
Familiar with `mysql_real_escape_string()` in `PHP` but not in `Python` can you show me an example of usage?
ian
Amber's answer is a better way of doing it than mine anyway - I'll leave my answer here only to show that there's more than one way.
thomasrutter
Bad Plan. Don't escape. Use bind variables.
S.Lott
+3  A: 

Let the MySQLdb module do the interpolation:

cursor.execute("""SELECT * FROM LINKS WHERE link = %s LIMIT 1""",
    (item['link'].encode("utf-8"),)
)

The execute() function can be passed items to be substituted into the query (see the documentation for execute()). It will automatically escape things as necessary for the DB query.

If you prefer to use a dict instead of a tuple to specify the things to substitute in:

cursor.execute("""SELECT * FROM LINKS WHERE link = %(link)s LIMIT 1""",
    {'link': item['link'].encode("utf-8")}
)
Amber
Causes errors at at `:` `_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '://thehood.raptorhideout.com/kingdom_come_again.mp3 LIMIT 1' at line 1")`
ian
Cancel that... just errors when in my version where the sql string is separate from the `execute`
ian
For the sake of completeness, one should note that the replacement characters (`%s`, `?`, `%(withdictionary)s`) vary across DB modules because [PEP 249](http://www.python.org/dev/peps/pep-0249/) allows different parameter styles.
AndiDog