0
可能重複:
tweepy stream to sqlite database - invalid synatxtweepy流SQLite數據庫 - 語法錯誤
我得到一個語法錯誤在我的代碼,我無法弄清楚什麼導致了它。這是控制檯返回的錯誤,沒有任何內容輸入到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)
仍然有同樣的問題,即使添加了額外的後?它也沒有寫入正確的信息到sqlite文件。 – 2012-02-27 22:08:56
@DavidNeudorfer我剛剛添加了關於使用'execute'的說明,這可能會有所幫助。 – Lycha 2012-02-27 22:12:06
它沒有工作。謝謝你的嘗試。 – 2012-02-27 22:36:34