问题描述
我正在尝试编写一个使用操"这个词的 reddit 机器人,看看有多少人在 reddit 上这么说.代码如下:
I am trying to write a reddit bot that takes the word "fuck" and see how many people say that on reddit. Here is the code:
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.
据我所知,这意味着数据库中的某些内容不是要插入到我的数据库中的正确数据类型.我很确定它是 SQL 设置的 str(comment.body)
部分.我的 SQLITE 数据库将注释字段作为文本字段.感谢您的帮助.
From what I understand it means that something in the database isnt the right datatype to be inserted into my database. I am pretty sure it is the str(comment.body)
section of the SQL setting. My SQLITE database has the comment field as a text field. Thanks for the help.
以下是数据库浏览器中显示的数据库凭据.
Here are the database credentials shown in DB Browser.
推荐答案
IntegrityError
结果来自使用 str(comment.id)
作为 ID 的插入语句
这是一个字符串,而不是架构要求的整数.
The IntegrityError
results from the insert statement using str(comment.id)
as ID
which is a string and not a integer as the schema requires.
但是因为那没有用.由于 comment.id
与您提供的 cogqssp
相同,您需要更改架构.ID
字段的类型为 TEXT
.然后您的原始代码将起作用.
But as that didn't work. As comment.id
is as you give cogqssp
, you'll need to change your schema. The field ID
to be of type TEXT
. And then your original code will work.
这篇关于IntegrityError:使用 praw 在 Python 中的数据类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!