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

刘易斯·梅内罗斯

我正在尝试编写一个带“ fuck”一词的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)

每当我运行该漫游器时,只要找到匹配项并给我这个错误,它都会崩溃。在此处输入图片说明

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

这是数据库浏览器中显示的数据库凭据。 在此处输入图片说明

和D。

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

但这是行不通的。comment.id您所愿cogqssp,您将需要更改架构。字段ID类型为TEXT然后您的原始代码将起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在python中匹配数据类型

数据类型不匹配

使用`BEFORE INSERT`触发器来更改传入数据的数据类型以匹配PostgreSQL中的列数据类型

SQL查询中的MS Access VBA数据类型不匹配错误

使用flatMap调用地图(用于Option数据类型),但在Scala中类型不匹配

LabVIEW数据库查询中“条件表达式中的数据类型不匹配”

Python中的数据类型错误匹配尝试使用某些Twitter数据更新Sqlite3数据库

在条件表达式中获取错误3464数据类型不匹配

尝试在集合中的对象中设置数据类型不匹配

使用RND()的数据类型不匹配

选择查询中出现错误:条件表达式中的数据类型不匹配

Jaydata中的数据类型不匹配

Access中的数据类型不匹配

VBA中SQL查询上的数据类型不匹配

我的控制器中Postgres数据类型不匹配的Heroku WHERE错误

vb.net中条件表达式中的数据类型不匹配

查询访问数据库时条件表达式中的数据类型不匹配

条件表达式中的数据类型与REPLACE不匹配,无法替换

返回数据类型不匹配

数据类型在函数调用中不匹配

“条件表达式中的数据类型不匹配”

CRITERIA EXPRESSION中的数据类型不匹配错误(ms Access)

数据类型不匹配Access 2010

标准表达式中的VB.NET数据类型不匹配

MS-Access SQL中的“条件表达式中的数据类型不匹配”

Excel SQL 数据类型不匹配

查询 SharePoint 列表时,VBA“查询表达式中的数据类型不匹配”

在 Swift 中匹配数据类型

OleDb.OleDbException 条件表达式中的数据类型不匹配