1

I have written the following code. I am so close to getting it to work, I can almost taste and smell it.

I am using mysql.connector, tweepy, python 3.2 and the xampp stack. I am creating a unique table to hold the last 3200 tweets from a user. I'm running it in a loop and I can get all the results to print to screen; absolutely no problem at all. The problem arises when I try to write to a MYSQL db. The table creates fine, as do the columns.

EDIT:

After assistance from @Yarkee and @bernie, I've edited it to the following:

tweet_created_date = str(tweet.created_at)
list = [tweet.id, tweet.text, tweet_created_date,
                    tweet.geo, tweet.contributors, tweet.coordinates,
                    tweet.favorited, tweet.in_reply_to_screen_name,
                    tweet.in_reply_to_status_id, tweet.in_reply_to_status_id_str,
                    tweet.in_reply_to_user_id, tweet.in_reply_to_user_id_str,
                    tweet.place, tweet.retweeted, tweet.retweet_count,
                    tweet.source, tweet.truncated]
sql = ("""INSERT INTO %(table_name)s (tweet_id, tweet_text,
    tweet_created_at, tweet_geo, tweet_contributors,
    tweet_coordinates, tweet_favorited,
    tweet_in_reply_to_screen_name, tweet_in_reply_to_status_id,
    tweet_in_reply_to_status_id_str, tweet_in_reply_to_user_id,
    tweet_in_reply_to_user_id_str, tweet_place,
    tweet_retweeted, tweet_retweet_count, tweet_source,
    tweet_truncated) VALUES (%s, %s, %s, %s, %s, %s, %s, %s,
    %s, %s, %s, %s, %s, %s, %s, %s, %s)""" % dict(table_name=table_name))
cursor.execute(sql, list)

I've now got a TypeError: not enough arguments for format string

Any ideas?

2
  • What does your database table look like?
    – summea
    Apr 2, 2013 at 16:36
  • 1
    see updated question.. I've added the code used to create it..
    – chowden
    Apr 2, 2013 at 16:40

4 Answers 4

1

MySQLdb uses the %s paramstyle, e.g.: (%s,%s,%s). It does NOT use the ? paramstyle. Reference: http://mysql-python.sourceforge.net/MySQLdb.html

2
  • 2
    your sql clause is end with "VALUES(?,?,?,?,...,?)" mysqldb doesn't suppport this format. It should be "VALUES(%s,%s,%s,...,%s)"
    – Yarkee
    Apr 2, 2013 at 17:01
  • I've edited the original question. I've now got another error message.
    – chowden
    Apr 2, 2013 at 21:45
1

You need to escape '%s' as '%%s'.

>>> '%(a)s, %s, %s' % dict(a='x')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> '%(a)s, %%s, %%s' % dict(a='x')
'x, %s, %s'
1
  • Absolutely spot on. You are a legend!
    – chowden
    Apr 2, 2013 at 23:48
0

Final code is as follows. The table name is generated separately and uniquely from a combination of the current unix time value and the user's screen name.

    tweet_list = [tweet.id, tweet.text, tweet.created_at, tweet.geo, 
    tweet.contributors, tweet.coordinates, tweet.favorited, 
    tweet.in_reply_to_screen_name, tweet.in_reply_to_status_id, 
    tweet.in_reply_to_status_id_str, tweet.in_reply_to_user_id, 
    tweet.in_reply_to_user_id_str, tweet.place, tweet.retweeted, tweet.retweet_count, 
    tweet.source, tweet.truncated]

    sql = ("""insert into %(table_name)s (tweet_id, tweet_text,
    tweet_created_at, tweet_geo, tweet_contributors, tweet_coordinates,
    tweet_favorited, tweet_in_reply_to_screen_name,
    tweet_in_reply_to_status_id, tweet_in_reply_to_status_id_str,
    tweet_in_reply_to_user_id, tweet_in_reply_to_user_id_str,
    tweet_place, tweet_retweeted, tweet_retweet_count, 
    tweet_source, tweet_truncated) VALUES (%%s,%%s,%%s,%%s,%%s,%%s,
    %%s,%%s,%%s,%%s, %%s,%%s,%%s,%%s,%%s,%%s,%%s)""" 
    % dict(table_name=table_name))

    cursor.execute(sql, tweet_list)

Thank you so much for all that have assisted and I hope that this helps someone in the future.

1
  • Glad I could help. It's good etiquette to accept an answer. Probably Yarkee's since he answered your original question.
    – ChrisGuest
    Apr 3, 2013 at 0:00
0

Using Python 3 (or even Python 2.6 or 2.7), you should take advantage of string.format(). Using a bit more simplified example, to show how to use it:

cnx = mysql.connector.connect(database='test')
cur = cnx.cursor()

stmt = "INSERT INTO {table} (c1) VALUES (%s)".format(
    table='t1'
) 
data = ('ham',)
cur.execute(stmt, data)
cnx.commit()

With format() you don't have to escape the %s parameter markers. (Note that I'm using MySQL Connector/Python, which was used in the original question.)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.