sqlite3没有插入表中

时间:2015-01-22 19:59:25

标签: python sqlite

我一直在使用PRAW来提取说“很多”的reddits评论。我正在尝试将其插入我正在使用的数据库中。

#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评论。但是,没有任何错误消息,它不会添加到我的数据库。数据库名称是alotUsers,表格很多。

2 个答案:

答案 0 :(得分:1)

检查 conn.commit 。你没有提交。

答案 1 :(得分:0)

conn.commit是一个函数,conn.commit()正在为我工​​作。谢谢@Cameron