Python MYSQL数据库更新语句问题

时间:2015-09-15 12:02:11

标签: python mysql

我正在尝试使以下Python MYSQL更新语句正确(使用变量):

try:
    connection = mysql.connector.connect\
       (host = "localhost", user = "root", passwd ="", db = "crawling")
except:
    print("Keine Verbindung zum Server")
    sys.exit(0)



cursor = connection.cursor()

cursor.execute("TRUNCATE meta;")
connection.commit()

cursor.execute("ALTER TABLE meta AUTO_INCREMENT =1;")
connection.commit()

for j in range(1, int(outerElements)):

    for i in range(1, int(innerElements)):

        partner_ID = 6

        location_ID = 20

        headline = driver.find_element_by_xpath("//div[@id='productList']/div["+str(j)+"]/div["+str(i)+"]/div/div[2]/h2/a").text

        price = driver.find_element_by_xpath("//div[@id='productList']/div["+str(j)+"]/div["+str(i)+"]/div/div[2]/div[2]/span[2]").text[:-1]

        deeplink = driver.find_element_by_xpath("//div[@id='productList']/div["+str(j)+"]/div["+str(i)+"]/div/div[2]/h2/a").get_attribute("href")





        print("Header: " + headline + " | " + "Price: " + price + " | " + "Deeplink: " + deeplink + " | " + "PartnerID: " + str(partner_ID) + " | " + "LocationID: " + str(location_ID))


        cursor.execute('''UPDATE meta SET (price_id, Header, Price, Deeplink, PartnerID, LocationID) \
          VALUES(%s, %s, %s, %s, %s, %s)''', ['None'] + [headline] + [price] + [deeplink] + [partner_ID] + [location_ID])

        connection.commit()


   cursor.close()
   connection.close()

执行代码时,我收到以下错误消息:

Traceback (most recent call last):
File "C:/Users/hmattu/PycharmProjects/untitled1/localhost_crawl.py", line  97, in test_sel
VALUES(%s, %s, %s, %s, %s, %s)''', ['None'] + [headline] + [price] +  [deeplink] + [partner_ID] + [location_ID])
File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 507, in   execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 722, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 640, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '= (price_id, Header, Price, Deeplink, PartnerID, LocationID)          

有人可以帮帮我吗?任何反馈都表示赞赏。

1 个答案:

答案 0 :(得分:0)

尝试更改

story        = 'A long foo ago, in a bar baz, baz away...'
search_terms = ['foo', 'bar', 'baz']
story.split(/\W+/).count { |word| search_terms.include? word }
  # => 4

UPDATE meta SET (price_id, Header, Price, Deeplink, PartnerID, LocationID)