无法写入sqlite3

时间:2014-12-08 05:45:42

标签: python sqlite

我正在写一个reddit机器人而我正试图写入一个sqlite3数据库,但程序只是立即崩溃。我正在关注一个youtube教程,一切都在进行,直到他开始用sqlite3做事。

import praw
import sqlite3

USERAGENT = "/u/***** reddit bot"
USERNAME = "*****"
PASSWORD = "*****"
SUBREDDIT = "test"
MAXPOSTS = 10

SETPHRASES = ["python", "bots"]
SETRESPONSE = "I am a python bot."

print('Opening database')
sql = sqlite3.connect('sql.db')
cur = sql.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS oldposts(ID TEXT)')
sql.commit()

print("Logging into Reddit.")
r = praw.Reddit(USERAGENT)
r.login(USERNAME,PASSWORD)

def replybot():
    print('Fetching subreddit ' + SUBREDDIT)
    subreddit = r.get_subreddit(SUBREDDIT)
    print('Fetching comments')
    comments = subreddit.get_comments(limit=MAXPOSTS)
    for comment in comments:
        cid = comment.id
        cur.execute('SELECT * FROM oldposts WHERE ID=?', [cid]
        if not cur.fetchone():
            try:
                cauthor.lower() = comment.author.name
                if cauthor.lower() != USERNAME.lower():
                    cbody = comment.body.lower()
                    if any(key.lower() in cbody for key in SETPHRASES):
                        print("Replying to " + cauthor)
                        comment.reply(SETRESPONSE)
            except AttributeError:
                pass

            cur.execute('INSERT INTO oldposts VALUES(?)',[cid])
            sql.commit()

replybot()

我做错了什么?我跟随的教程说我所要做的就是导入sqlite3,它会起作用。

0 个答案:

没有答案