5

私は「性交」という単語を取る reddit ボットを作成しようとしています。コードは次のとおりです。

import praw
import time
import re
import sqlite3

username = "LewisTheRobot"
password = "lewismenelaws"

conn = sqlite3.connect('reddit')
c = conn.cursor()
r = praw.Reddit(user_agent = "Reddit bot")

word_to_match = [r'\bfuck\b', r'\bfucking\b', r'\bfucks\b']

storage = []

r.login(username, password)

def run_bot():
    subreddit = r.get_subreddit("all")
    print("Grabbing subreddit!")
    comments = subreddit.get_comments(limit=200)
    print("Grabbing comments!")
    for comment in comments:
        comment_text = comment.body.lower()
        isMatch = any(re.search(string, comment_text) for string in word_to_match)
        if comment.id not in storage and isMatch and comment.author not in storage:
            print("We have found a fuck boy. Storing username: " + str(comment.author) + "into database.")
            storage.append(comment.author)
            c.execute("INSERT INTO users (id, Username, subreddit, comment) VALUES(?,?,?,?)", (str(comment.id), str(comment.author), str(comment.subreddit), str(comment.body)))
            conn.commit()

    print("There are currently " + str(len(storage)) + " fuck boys on reddit at the moment.")



while True:
    run_bot()
    time.sleep(2)

ボットを実行すると、一致するものが見つかるたびにクラッシュし、このエラーが発生します。ここに画像の説明を入力

私が理解していることから、データベース内の何かがデータベースに挿入される正しいデータ型ではないことを意味します。str(comment.body)SQL設定のセクションだと確信しています。私の SQLITE データベースには、テキスト フィールドとしてコメント フィールドがあります。助けてくれてありがとう。

DB Browser に表示されるデータベース資格情報は次のとおりです。 ここに画像の説明を入力

4

2 に答える 2

1

わかりました。ID フィールドのデータ型は整数でしたが、コメントの reddit の ID です。例: 「cogr0o4」。そこで、それをデータ型「TEXT」に変更し、SQLステートメントで文字列に変換しました。

于 2015-02-10T04:37:24.127 に答える