0

I tried all possible suggestions and no results.. This is my code

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

Tried all suggested variations on stackoverflow, python docs, mysql docs.... Already spent like 6 hours to find out what is going on.. I still get this error..

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

What is going on? Why is it not happy with %s.. Please help and please no answers of the type "have you tried... have you read... because I did.. I am sure it's simple thing i am just missing somewhere"

3
  • It is an integer. In the database it's set as int(11) Commented Mar 17, 2015 at 21:57
  • Same thing happened. Commented Mar 17, 2015 at 22:04
  • You should look at tuple unpacking to read the values from T.values() more neatly e.g. userid, scrname, timestmp, hashtag, text, geo, id, data = T.values() instead of all those lines and ugly indices. Commented Mar 17, 2015 at 22:17

1 Answer 1

2

You should pass a tuple by adding an extra comma:

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

From the 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.

Sign up to request clarification or add additional context in comments.

2 Comments

God Bless you ! Damn 6 hours for a comma... Well done by me :)
Yes I will bear that in mind for future. However this is just for the question I left with one value. Really thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.