Escaping confusion with Python 3 + MySQL

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Sun Mar 26 04:52:43 EDT 2017


Νίκος Βέργος writes:

> print('''UPDATE visitors SET (pagesID, host, ref, location, useros,
> browser, visits) VALUES (%s, %s, %s, %s, %s, %s, %s) WHERE host LIKE
> "%s"''', (pID, domain, ref, location, useros, browser, lastvisit,
> domain) )
>
> prints out:
>
> UPDATE visitors SET (pagesID, host, ref, location, useros, browser,
> visits) VALUES (%s, %s, %s, %s, %s, %s, %s) WHERE host LIKE "%s" (1,
> 'cyta.gr', 'Άμεση Πρόσβαση', 'Greece', 'Windows', 'Chrome', '17-03-24
> 22:04:24', 'cyta.gr')
>
> How should i write the cursor.execute in order to be parsed properly?
> As i have it now %s does not get substituted.
> i use PyMySQL by the way and i have tried every possible combination
> even with % instead of a comma but still produces errors.

You should include the actual statement that produces the errors, not a
very different statement. And you should include the actual text of the
error messages.

To learn about PyMySQL cursor.execute, put "pymysql cursor execute"
(without the quotes) to a search engine and find something like the
following document (the first hit from google.fi for me).

https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html

That looks helpful to me.



More information about the Python-list mailing list