python - 完整性错误 : datatype mismatch in Python using praw

标签 python sqlite praw

我正在尝试编写一个使用“操”这个词的 reddit 机器人,看看有多少人在 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)

每当我运行机器人时,只要它找到匹配项就会崩溃并给我这个错误。 enter image description here

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

这是数据库浏览器中显示的数据库凭据。 enter image description here

最佳答案

IntegrityError 是插入语句使用 str(comment.id) 作为 ID 的结果,ID 是字符串而不是整数作为架构需要。

但是因为那没有用。由于 comment.id 与您提供的 cogqssp 相同,因此您需要更改架构。 ID 字段的类型为 TEXT。然后您的原始代码将起作用。

关于python - 完整性错误 : datatype mismatch in Python using praw,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28423982/

相关文章:

python GTK : Scrollable Grid with clickable images

java - 测试串行 SQLite JDBC 时出现 NoClassDefFoundError 错误

ubuntu - 我如何安装虾

python - 在 Python 中处理超时异常

python - 虾属性错误: can't set attribute

python - 如何在 Python 中迭代 Queue.Queue 项目?

python - 在 Sphinx 中自动为 autodoc 类创建目录树

python - 在文档过程中使用 re 捕获关键字之间的文本

java - 如何在另一个 Activity 的 GraphView 中将 SQLite 数据库中的日期显示为 X 轴,仅显示 1-1-1970?

java - 我在哪里可以找到 android studio 中的数据库和表记录?