Why does SQL query written in python gives me an error?
Question:
I’m using selenium and undetected driver to scarp some information from website and compare them with data stored in MySQL MariaDB server on my vps. I done everything according to this tutorial: https://www.w3schools.com/python/python_mysql_where.asp
After all it looks like this
query = "SELECT polski, niemiecki from tlumaczenie where polski = %s"
translate = driver.find_element(By.CSS_SELECTOR, "div[class='translations']").text
cursor.execute(query, translate)
res = cursor.fetchall()
print(res)
Whenever I run this code I’m getting an error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘%s’ at line 1′
I tried to adding ‘ and " tags next to %s but then all I’m getting is empty list even if data are stored in table.
Answers:
Your 2nd argument to cursor.execute
is a string, it needs to be a tuple.
Notice in the example you linked to, they use adr = ("Yellow Garden 2", )
, that’s how you create a single-element tuple in Python
Try this:
cursor.execute(query, (translate,))
I’m using selenium and undetected driver to scarp some information from website and compare them with data stored in MySQL MariaDB server on my vps. I done everything according to this tutorial: https://www.w3schools.com/python/python_mysql_where.asp
After all it looks like this
query = "SELECT polski, niemiecki from tlumaczenie where polski = %s"
translate = driver.find_element(By.CSS_SELECTOR, "div[class='translations']").text
cursor.execute(query, translate)
res = cursor.fetchall()
print(res)
Whenever I run this code I’m getting an error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘%s’ at line 1′
I tried to adding ‘ and " tags next to %s but then all I’m getting is empty list even if data are stored in table.
Your 2nd argument to cursor.execute
is a string, it needs to be a tuple.
Notice in the example you linked to, they use adr = ("Yellow Garden 2", )
, that’s how you create a single-element tuple in Python
Try this:
cursor.execute(query, (translate,))