【问题标题】:sqlite3 isn't inserting into tablesqlite3 没有插入到表中
【发布时间】:2015-01-22 19:59:24
【问题描述】:

我一直在使用 PRAW 来拉取说“很多”的 reddits cmets。我正在尝试将它插入到我正在使用的数据库中。

#importing praw for reddit api and time to make intervals

import praw
import time
import re
import sqlite3

username = "LewisTheRobot"
password = ""

conn = sqlite3.connect('alotUsers')
c = conn.cursor()
r = praw.Reddit(user_agent = "Counts people who say alot")

word_to_match = [r'\balot\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("Match found! Storing username: " + str(comment.author) + " into list.")
            storage.append(comment.author)
            c.execute("INSERT INTO alot (id, username) VALUES(?, ?)", (str(comment.id), str(comment.author)))
            conn.commit

    print("There are currently: " + str(len(storage)) + " people who use 'alot' instead of ' a lot'.")


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

目前它会添加到列表中并找到说很多的 reddit cmets。但是,没有任何错误消息,它不会添加到我的数据库中。数据库名称是 alotUsers,表是 alot。

【问题讨论】:

  • conn.commit 是一个函数。您检索它,但从不调用它。请改用conn.commit()。

标签: python sqlite


【解决方案1】:

检查 conn.commit 。你不承诺。

【讨论】:

    【解决方案2】:

    conn.commit 是一个函数,conn.commit() 对我有用。谢谢@Cameron

    【讨论】:

      猜你喜欢
      • 2011-02-06
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 2017-07-11
      • 2020-06-10
      • 2018-01-26
      相关资源
      最近更新 更多