我尝试了所有可能的建议,但没有结果。这是我的代码
class Tweet(dict):
def __init__(self, raw_tweet):
super(Tweet, self).__init__(self)
if raw_tweet and 'delete' not in raw_tweet:
self['user_id'] = raw_tweet['user']['id']
self['screen_name'] = raw_tweet['user']['screen_name']
self['timestamp'] = dateutil.parser.parse(raw_tweet[u'created_at']
).replace(tzinfo=None).isoformat()
self['hashtags'] = [x['text'] for x in raw_tweet['entities']['hashtags']]
self['text'] = raw_tweet['text']
self['geo'] = raw_tweet['geo']['coordinates'] if raw_tweet['geo'] else None
self['id'] = raw_tweet['id']
if REALTIME_DATA:
T = None
while not T:
T = Tweet(stream.next())
else:
T = Tweet(json.load(open('one_tweet.json')))
print json.dumps(T, sort_keys=True, indent=2)
userid = T.values()[0]
scrname = T.values()[1]
timestmp = T.values()[2]
hashtag = T.values()[3]
text = T.values()[4]
geo = T.values()[5]
id = T.values()[6]
data = (2)
c.execute("insert into tweets (user_id) values (%s)", (userid))
cnx.commit()
尝试了所有建议的stackoverflow,python docs,mysql docs的变体。。。已经花了6个小时来弄清楚到底发生了什么。我仍然得到这个错误。
Traceback (most recent call last):
File "C:\Users\Lizard\workspace\final_project\main.py", line 69, in <module>
c.execute("insert into tweets (user_id) values (%s)", (userid))
File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 507, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 722, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Python27\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 '%s)' at line 1
怎么回事?为什么对%s不满意..请帮助,请不要回答“你有没有试过...你读过...因为我做了我确信这是一件简单的事情,我只是在某个地方错过了”
1条答案
按热度按时间abithluo1#
您应该通过添加额外的逗号来传递元组:
从文档:
一个特殊的问题是构造包含0或1项的元组:语法有一些额外的怪癖来适应这些。空元组由一对空括号构成;只有一个元素的元组是通过在一个值后面加一个逗号来构造的(用括号括住一个值是不够的)。丑陋,但有效。