user2876777
user2876777

Reputation: 13

Python Syntax for Update SQL query

I want to update a table with the following query. I am getting multiple errors on the following. What is the correct syntax for writing the query below

 cursor.execute("""UPDATE `%s` SET `content`=%s WHERE link=%s""", (feed,cnews,news_url))

The error I get when running the above is

Traceback (most recent call last):
  File "digger_1.py", line 34, in <module>
    cursor.execute("""UPDATE `%s` SET `content`=%s WHERE link=%s""", (feed,cnews,news_url))
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1146, "Table 'newstracker.'NDTV'' doesn't exist")

The error I picked was the table newstracker.NDTV doesn't exist, which is not right as it does exist, and I believe the error is something else which is wrong with the syntax.

Upvotes: 0

Views: 488

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

You cannot specify metadata such as database, table, or field names using a parametrized query. You must substitute them using normal string formatting and then use the result string as your parametrized query.

...("""UPDATE `%s` SET `content`=%%s WHERE link=%%s""" % (feed,), ...)

Upvotes: 2

Related Questions