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 は paramstyle を使用し%sます(%s,%s,%s)?paramstyleは使用しません。参照: http://mysql-python.sourceforge.net/MySQLdb.html

于 2013-04-02T16:47:35.150 に答える
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 Connector/Python を使用していることに注意してください。)

于 2013-07-09T08:56:21.957 に答える