tweepy 流到 sqlite 数据库

编程入门 行业动态 更新时间:2024-10-27 04:34:29
本文介绍了tweepy 流到 sqlite 数据库 - 语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能的重复: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 数据库

本文发布于:2023-10-28 00:41:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流到   数据库   tweepy   sqlite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!