Python 2.7 错误,%s

Python 2.7 error with %s

我尝试了所有可能的建议但没有结果.. 这是我的代码

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()

尝试了所有建议的 Whosebug、python 文档、mysql 文档....已经花了大约 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 不满意.. 请帮忙,请不要回答 "have you tried... have you read... because I did.. I am sure it's simple thing i am just missing somewhere"

类型的问题

您应该通过添加一个额外的逗号来传递一个元组:

c.execute("insert into tweets (user_id) values (%s)", (userid,))

来自docs

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective.