IntegrityError:使用 praw 在 Python 中数据类型不匹配

2024-04-08

我正在尝试编写一个 Reddit 机器人,它接受“fuck”这个词,看看有多少人在 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)

Whenever I run the bot it crashes whenever it finds a match and gives me this error.enter image description here

据我了解,这意味着数据库中的某些内容不是插入到我的数据库中的正确数据类型。我很确定这是str(comment.body)SQL 设置部分。我的 SQLITE 数据库将注释字段作为文本字段。谢谢您的帮助。

Here are the database credentials shown in DB Browser. enter image description here


The IntegrityError使用插入语句的结果str(comment.id) as ID这是一个字符串,而不是架构所需的整数。

但因为那不起作用。作为comment.id正如你所给予的cogqssp,您需要更改您的架构。场ID成为类型TEXT。然后你的原始代码就可以工作了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IntegrityError:使用 praw 在 Python 中数据类型不匹配 的相关文章

随机推荐