可能的重复:tweepy 流到 sqlite 数据库 - 无效的 Synatx
我的代码出现语法错误,我不知道是什么原因造成的.这是控制台返回的错误,并且没有向 sqlite 文件输入任何内容.
I'm getting a syntax error in my code and I can't figure out what's causing it. This is the error the console is returning and nothing is being inputed to the sqlite file.
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 文件只有:
my sqlite file only has:
... tableTWEETSTWEETSCREATE TABLE TWEETS(txt text, author text, created int, source text)你们能帮我弄清楚我做错了什么吗?谢谢.代码如下.
Can you guys help me figure out what I'm doing wrong? Thanks. The code is below.
import sys import tweepy import webbrowser import sqlite3 as lite # Query terms Q = sys.argv[1:] sqlite3file='/var/www/twitter.lbox/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 上,您有 3 个?"标记,但有 4 个参数.它可能缺少一个额外的问号.你也应该使用 execute 而不是 executemany 因为你只做一次插入.像这样:
On the executemany you have three "?"-marks but 4 parameters. It is probably missing an additional questionmark. Also you should probably just use execute instead of executemany since you only do one insert. Like this:
cur.execute("INSERT INTO TWEETS(?, ?, ?, ?)", (status.text, status.author.screen_name, status.created_at, status.source))根据 this 的正确 SQL 也是:
Also the correct SQL according to this would be:
INSERT INTO TWEETS VALUES(?, ?, ?, ?)更多推荐
tweepy 流到 sqlite 数据库
发布评论