python操作json到mysql_使用Python将JSON插入到MySQL中

我在

Python中有一个JSON对象.我使用Python DB-API和SimpleJson.我试图将json插入MySQL表.

当时我收到错误,我相信这是由于JSON对象中的单引号.

如何使用Python将JSON对象插入到MySQL中?

这是我得到的错误消息:

error: uncaptured python exception, closing channel

0x7ff68f91d7e8> (:

(1064, "You have an error in your SQL syntax; check the

manual that corresponds to your MySQL server version for

the right syntax to use near ''favorited': '0',

'in_reply_to_user_id': '52063869', 'contributors':

'NULL', 'tr' at line 1")

[/usr/lib/python2.5/asyncore.py|read|68]

[/usr/lib/python2.5/asyncore.py|handle_read_event|390]

[/usr/lib/python2.5/asynchat.py|handle_read|137]

[/usr/lib/python2.5/site-packages/twitstream-0.1-py2.5.egg/

twitstream/twitasync.py|found_terminator|55] [twitter.py|callback|26]

[build/bdist.linux-x86_64/egg/MySQLdb/cursors.py|execute|166]

[build/bdist.linux-x86_64/egg/MySQLdb/connections.py|defaulterrorhandler|35])

另一个错误供参考

error: uncaptured python exception, closing channel

0x7feb9d52b7e8> (:

(1064, "You have an error in your SQL syntax; check the manual

that corresponds to your MySQL server version for the right

syntax to use near 'RT @tweetmeme The Best BlackBerry Pearl

Cell Phone Covers http://bit.ly/9WtwUO''' at line 1")

[/usr/lib/python2.5/asyncore.py|read|68]

[/usr/lib/python2.5/asyncore.py|handle_read_event|390]

[/usr/lib/python2.5/asynchat.py|handle_read|137]

[/usr/lib/python2.5/site-packages/twitstream-0.1-

py2.5.egg/twitstream/twitasync.py|found_terminator|55]

[twitter.py|callback|28] [build/bdist.linux-

x86_64/egg/MySQLdb/cursors.py|execute|166] [build/bdist.linux-

x86_64/egg/MySQLdb/connections.py|defaulterrorhandler|35])

#!/usr/bin/env python

try:

import json as simplejson

except ImportError:

import simplejson

import twitstream

import MySQLdb

USER = ''

PASS = ''

USAGE = """%prog"""

conn = MySQLdb.connect(host = "",

user = "",

passwd = "",

db = "")

# Define a function/callable to be called on every status:

def callback(status):

twitdb = conn.cursor ()

twitdb.execute ("INSERT INTO tweets_unprocessed (text, created_at, twitter_id, user_id, user_screen_name, json) VALUES (%s,%s,%s,%s,%s,%s)",(status.get('text'), status.get('created_at'), status.get('id'), status.get('user', {}).get('id'), status.get('user', {}).get('screen_name'), status))

# print status

#print "%s:\t%s\n" % (status.get('user', {}).get('screen_name'), status.get('text'))

if __name__ == '__main__':

# Call a specific API method from the twitstream module:

# stream = twitstream.spritzer(USER, PASS, callback)

twitstream.parser.usage = USAGE

(options, args) = twitstream.parser.parse_args()

if len(args) < 1:

args = ['Blackberry']

stream = twitstream.track(USER, PASS, callback, args, options.debug, engine=options.engine)

# Loop forever on the streaming call:

stream.run()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值