插入ERROR - mysql连接器python 2.7

时间:2017-06-20 10:38:00

标签: python sql-insert mysql-connector

我正在尝试将一些情绪分析结果(谷歌云语言API)插入到mysql数据库中。我使用的是mysql connector

import mysql.connector
from google.cloud import language

cnx = mysql.connector.connect(user='blahuser', password='Blahpw',
                          host='BlahIP',
                          database='FeedbackDB')

cursor = cnx.cursor(buffered=True)
CoreSQL = ("SELECT ResID, TextResp FROM Response")
cursor.execute(CoreSQL)
client = language.Client()

for row in cursor:
    document = client.document_from_text(row[1])
    sent_analysis = document.analyze_sentiment()
    sentiment = sent_analysis.sentiment
    annotations = document.annotate_text(include_sentiment=True, include_syntax=True, include_entities=True)
    print(row[0], sentiment.score, sentiment.magnitude)
    ResID_ =row[0]
    PhraseSent_ = sentiment.score
    PhraseMag_ = sentiment.magnitude
    SQLInsertCmd = ("INSERT INTO PhraseAnalysis (ResID, PhraseSent, PhraseMag),  VALUES (%s,%s,%s)");
    cursor.execute(SQLInsertCmd, (ResID_, PhraseSent_,PhraseMag_))

cnx.commit()
cursor.close()
cnx.close()

我得到的错误表明我的INSERT语句存在问题:

  

python tm16.py (1, -0.4, 2.2) Traceback (most recent call last): File "tm16.py", line 27, in <module> cursor.execute(SQLInsertCmd, (ResID_, PhraseSent_,PhraseMag_)) File "/usr/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 559, in execute self._handle_result(self._connection.cmd_query(stmt)) File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 494, in cmd_query result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query)) File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 396, 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 ' VALUES (1,-0.4,2.2)' at line 1

网上有很多INSERT示例,但我无法解决。编码新手 - 毫无疑问是简单的。有人能指出我哪里错了吗?

麦克

1 个答案:

答案 0 :(得分:0)

在字段列表结束后,您有一个不必要的逗号。