gpt4 book ai didi

python - tweepy流到sqlite数据库-语法错误

转载 作者:行者123 更新时间:2023-12-03 08:24:05 25 4
gpt4 key购买 nike

这个问题已经在这里有了答案:




已关闭8年。




Possible Duplicate:
tweepy stream to sqlite database - invalid synatx



我的代码中出现语法错误,无法弄清楚是什么原因引起的。这是控制台返回的错误,没有将任何内容输入到sqlite文件。
Filtering the public timeline for "@lunchboxhq"
RT @LunchboxHQ: @lunchboxhq test1 LunchboxHQ 2012-02-27 17:26:14 Echofon
Encountered Exception: near "?": syntax error
@LunchboxHQ test 1 LunchboxHQ 2012-02-27 17:26:36 Echofon
Encountered Exception: near "?": syntax error
@LunchboxHQ test 2 LunchboxHQ 2012-02-27 17:26:51 Echofon
Encountered Exception: near "?": syntax error

我的sqlite文件只有:
... tableTWEETSTWEETSCREATE TABLE TWEETS(txt text, author text, created int, source text)

你们能帮我弄清楚我做错了什么吗?谢谢。代码如下。
import sys
import tweepy
import webbrowser
import sqlite3 as lite

# Query terms

Q = sys.argv[1:]

sqlite3file='/var/www/twitter.lbox.com/html/stream5_log.sqlite'

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

con = lite.connect(sqlite3file)
cur = con.cursor()
cur.execute("CREATE TABLE TWEETS(txt text, author text, created int, source text)")

class CustomStreamListener(tweepy.StreamListener):

def on_status(self, status):

try:
print "%s\t%s\t%s\t%s" % (status.text,
status.author.screen_name,
status.created_at,
status.source,)

cur.executemany("INSERT INTO TWEETS(?, ?, ?, ?)", (status.text,
status.author.screen_name,
status.created_at,
status.source))

except Exception, e:
print >> sys.stderr, 'Encountered Exception:', e
pass

def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream

def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream

streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)

print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)

streaming_api.filter(follow=None, track=Q)

最佳答案

executemany上,您具有三个“?”标记和四个参数。它可能缺少一个额外的问号。另外,您可能只应使用execute而不是executemany,因为您仅执行一次插入。像这样:

cur.execute("INSERT INTO TWEETS(?, ?, ?, ?)", (status.text, 
status.author.screen_name,
status.created_at,
status.source))

同样根据 this的正确SQL将是:
INSERT INTO TWEETS VALUES(?, ?, ?, ?)

关于python - tweepy流到sqlite数据库-语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9470308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com