1

我已经编写了以下代码。我非常接近让它工作,我几乎可以品尝和闻到它。

我正在使用 mysql.connector、tweepy、python 3.2 和 xampp 堆栈。我正在创建一个独特的表来保存来自用户的最后 3200 条推文。我正在循环运行它,我可以将所有结果打印到屏幕上;绝对没有问题。当我尝试写入 MYSQL 数据库时出现问题。该表创建良好,列也是如此。

编辑:

在@Yarkee 和@bernie 的帮助下,我将其编辑为以下内容:

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)

我现在有一个TypeError: not enough arguments for format string

有任何想法吗?

4

4 回答 4

1

MySQLdb 使用%sparamstyle,例如:(%s,%s,%s). 它不使用?paramstyle。参考: http: //mysql-python.sourceforge.net/MySQLdb.html

于 2013-04-02T16:47:35.150 回答
1

您需要'%s''%%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'
于 2013-04-02T22:54:54.640 回答
0

最终代码如下。表名是根据当前 unix 时间值和用户的屏幕名称的组合单独且唯一地生成的。

    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)

非常感谢您提供的所有帮助,我希望这对将来的某人有所帮助。

于 2013-04-02T23:52:35.187 回答
0

使用 Python 3(甚至 Python 2.6 或 2.7),您应该利用 string.format()。使用更简化的示例来展示如何使用它:

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

使用 format() 您不必转义 %s 参数标记。(请注意,我使用的是原始问题中使用的 MySQL 连接器/Python。)

于 2013-07-09T08:56:21.957 回答