当前位置: 首页 > 面试题库 >

使用Python将JSON插入MySQL

马朝斑
2023-03-14
问题内容

我在Python中有一个JSON对象。我正在使用Python DB-API和SimpleJson。我正在尝试将json插入MySQL表中。

目前出现错误,我相信这是由于JSON对象中的单引号引起的。

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

这是我收到的错误消息:

error: uncaptured python exception, closing channel 
<twitstream.twitasync.TwitterStreamPOST connected at 
0x7ff68f91d7e8> (<class '_mysql_exceptions.ProgrammingError'>:
(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 
<twitstream.twitasync.TwitterStreamPOST connected at 
0x7feb9d52b7e8> (<class '_mysql_exceptions.ProgrammingError'>:
(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])

这是我正在使用http://pastebin.com/q5QSfYLa的代码的链接

#!/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()

问题答案:

使用json.dumps(json_value)在json字符串中转换json对象(python对象),您可以在mysql的文本字段中插入

http://docs.python.org/library/json.html



 类似资料:
  • 我在postgreSQL DB(9.2)中有一个表,其中列类型为json。我正在使用Hibernate,并实现了Tim Fulmer建议的将postgreSQL JSON列映射到Hibernate值类型的代码示例,但我不断得到以下异常: 我不想强制转换表达式,因为不建议使用它。当我使用预先生成的表并且列类型为JSON时,我得到了这个错误。当我更改Hibernate配置以构建表时,我没有得到异常,值

  • 问题内容: 我已经使用hive在hbase中创建了一个表: 并创建了另一个表来加载数据: 最后将数据插入到hbase表中: 该表在hbase中如下所示: 我可以对JSON文件做同样的事情: 并做: 请帮忙 !:) 问题答案: 您可以使用该函数将数据解析为JSON对象。例如,如果您使用JSON数据创建登台表: 然后使用提取要加载到表中的属性: 有此功能的更全面的讨论在这里。

  • 我正在努力将JSON对象插入我的postgres v9.4 DB。我已经将名为“evtjson”的列定义为类型json(而不是jsonb)。 我试图在Java(jdk1.8)中使用准备好的语句将Json对象(使用JEEjavax.json库构建)插入到列中,但我一直遇到SQLException错误。 我使用以下方法创建JSON对象: 然后,我将这个对象作为参数传递给另一个方法,以使用准备好的语句将

  • 父文档 我希望首先创建一个文档{“key”:“key-1”,“value”:“100”},然后将该文档映射到父文档的max键,类似地将另一个文档映射到min键。 我试了很多方法,但我不能使它发挥作用。

  • 问题内容: 我想在数据库中插入一个列表,但是不能。 这是我需要的一个例子: 可以这样做吗?我可以插入列表作为值吗?当我尝试它时,出现错误提示,这是因为MySQL语法错误 问题答案: 原始问题的答案是:不,您不能插入这样的列表。 但是,通过一些调整,您可以通过使用并传入一个元组来使代码工作: 不幸的是,这种变量插入样式使您的代码容易受到SQL注入攻击的攻击。 相反,我们建议使用Python的DB A

  • 问题内容: 我读到这里:使用Python将CSV文件导入sqlite3数据库表 似乎每个人都建议使用逐行读取,而不是使用SQLite的bulk .import。但是,如果您有数百万行的数据,这将使插入速度非常慢。还有其他方法可以避免这种情况吗? 更新:我尝试下面的代码逐行插入,但是速度不如我预期的那样。反正有改进的地方吗 问题答案: 使用生成器表达式将您的数据即时划分为多个块,在事务内进行插入。这