mysql出现4200错误,mysql.connector.errors.ProgrammingError:1064(4200):您的SQL语法有错误;...

I am attempting to insert data into a MySQL database. I am using python 2.7 and I am using the mysql.connector.

My error is:

mysql.connector.errors.ProgrammingError: 1064 (4200): 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 '%s) at line 1.

This indicates a error in my code where I attempt to insert my variable np (VALUES (%s)");). np is a "noun-phrase" such as a "skate board".

import mysql.connector

from textblob import TextBlob

cnx = mysql.connector.connect(user='XXX', password='XXX',

host='XXXXX',

database='XXXX')

cursor = cnx.cursor(buffered=True)

Latest = ("SELECT * FROM SentAnalysis")

cursor.execute(Latest)

for row in cursor.fetchall():

SentText = row[2]

blob = TextBlob(SentText)

for np in blob.noun_phrases:

print(np)

SQLInsertCmd = ("INSERT INTO TestNounPhrase (NPhrase) VALUES (%s)")

cursor.execute(SQLInsertCmd,np)

cnx.commit()

cursor.close()

cnx.close()

"VALUES (%s, %s, %s, %s, %s)")

I can't see a difference. This error is also discussed in detail here : How can I fix MySQL error #1064? Other similar examples on stackoverflow have been linked to reserved words, Redundant comas .But looking at these examples I can't spot an obvious error.

Any suggestions on where I am going wrong would be much appreciated.

解决方案

str parameter in SQL query must by in quote 'str'.

So, need use '%s' with ' ' for str, and %s without ' ' for numbers:

cursor.execute("""INSERT INTO db_name (str,int,str,int)

VALUES ('%s', %s, '%s', %s)""" % (str,int,str,int))

cnx.commit()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值