2015-09-15 54 views
-1

我試圖讓下面的Python MYSQL更新語句正確的是(使用變量):當進行代碼,I'm得到以下錯誤消息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

該錯誤不符合您的代碼。它的意思是你的UPDATE語句中的括號之前有一個'=',它不在你發佈的代碼中。 –

+1

另外,'['None'] + [headline] + [price] ...'是無稽之談:列表可以有多個項目,這就是整個點。 '['None',headline,price ...]' –

回答

0

嘗試改變

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

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

不幸的是,它仍然不能正常工作: mysql.connector.errors.ProgrammingError:1064(42000):你的SQL語法錯誤;請檢查第1行 –

+0

處的'('price_id','Header','Price','Deeplink','PartnerID','LocationID')附近使用的正確語法對應的MySQL服務器版本的手冊。 ,而不是'。 'cursor.execute''''UPDATE meta SET('price_id','Header','Price','Deeplink','PartnerID','LocationID')\ VALUES(%s,%s,%s, %),%s,%s)'''%('None',headline,price,deeplink,partner_ID,location_ID]))' – yrykde

+0

@SeriousRuffy您從哪裏得到'()VALUES()'語法?它只對'INSERT'(也許'REPLACE')有效,但對'UPDATE'不適用。 – glglgl