使用Python将JSON插入MySQL

时间:2010-11-22 23:07:04

标签: python mysql json python-db-api

我在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()

9 个答案:

答案 0 :(得分:18)

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

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

答案 1 :(得分:4)

扩展其他答案:

基本上你需要确定两件事:

  1. 您有足够的空间存放要在其尝试放置的字段中插入的全部数据。不同的数据库字段类型可以适合不同数量的数据。 见:MySQL String Datatypes。您可能需要“TEXT”或“BLOB”类型。

  2. 您正在安全地将数据传递到数据库。传递数据的某些方法可能导致数据库“查看”数据,如果数据看起来像SQL,它将会混淆。这也是一种安全风险。请参阅:SQL Injection

  3. #1的解决方案是检查数据库的设计是否具有正确的字段类型。

    #2的解决方案是使用参数化(绑定)查询。例如,而不是:

    # Simple, but naive, method.
    # Notice that you are passing in 1 large argument to db.execute()
    db.execute("INSERT INTO json_col VALUES (" + json_value + ")")
    

    更好,使用:

    # Correct method. Uses parameter/bind variables.
    # Notice that you are passing in 2 arguments to db.execute()
    db.execute("INSERT INTO json_col VALUES %s", json_value)
    

    希望这会有所帮助。如果是这样,请告诉我。的: - )

    如果您仍然遇到问题,我们需要更仔细地检查您的语法。

答案 2 :(得分:1)

您应该能够轻松地插入文本或blob列

db.execute("INSERT INTO json_col VALUES %s", json_value)

答案 3 :(得分:0)

错误可能是由于您尝试插入json的字段大小溢出造成的。没有任何代码,很难帮助你。

您是否考虑过一个非sql数据库系统,例如couchdb,这是一个依赖于json格式的面向文档的数据库?

答案 4 :(得分:0)

你需要看看实际的SQL字符串,尝试这样的事情:

sqlstr = "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 "about to execute(%s)" % sqlstr
twitdb.execute(sqlstr)

我想你会在那里找到一些杂散的引号,括号或括号。

答案 5 :(得分:0)

@route('/shoes', method='POST')
def createorder():
    cursor = db.cursor()
    data = request.json
    p_id = request.json['product_id']
    p_desc = request.json['product_desc']
    color = request.json['color']
    price = request.json['price']
    p_name = request.json['product_name']
    q = request.json['quantity']
    createDate = datetime.now().isoformat()
    print (createDate)
    response.content_type = 'application/json'
    print(data)
    if not data:
        abort(400, 'No data received')

    sql = "insert into productshoes (product_id, product_desc, color, price, product_name,         quantity, createDate) values ('%s', '%s','%s','%d','%s','%d', '%s')" %(p_id, p_desc, color, price, p_name, q, createDate)
    print (sql)
    try:
    # Execute dml and commit changes
        cursor.execute(sql,data)
        db.commit()
        cursor.close()        
    except:
    # Rollback changes
        db.rollback()
    return dumps(("OK"),default=json_util.default)

答案 6 :(得分:0)

将python映射插入MySQL JSON字段的最直接方法...

python_map = { "foo": "bar", [ "baz", "biz" ] }

sql = "INSERT INTO your_table (json_column_name) VALUES (%s)"
cursor.execute( sql, (json.dumps(python_map),) )

答案 7 :(得分:0)

如果您想编写一些内联代码,比如不带import json的小json值,这里有个快速提示。 您可以通过在双引号中对SQL中的引号进行转义,即使用''""输入'"

示例Python代码(未经测试):

q = 'INSERT INTO `table`(`db_col`) VALUES ("{k:""some data"";}")'
db_connector.execute(q)

答案 8 :(得分:0)

一个示例,如何使用JSONMySQL文件添加到Python中。这意味着有必要将JSON文件转换为sql insert,如果有多个JSON对象,那么最好只有一个调用INSERT,而不是多个调用,即为每个对象调用函数INSERT INTO

# import Python's JSON lib
import json

# use JSON loads to create a list of records
test_json = json.loads('''
[
    {
    "COL_ID": "id1",
    "COL_INT_VAULE": 7,
    "COL_BOOL_VALUE": true,
    "COL_FLOAT_VALUE": 3.14159,
    "COL_STRING_VAULE": "stackoverflow answer"
    },
    {
    "COL_ID": "id2",
    "COL_INT_VAULE": 10,
    "COL_BOOL_VALUE": false,
    "COL_FLOAT_VALUE": 2.71828,
    "COL_STRING_VAULE": "http://stackoverflow.com/"
    },
    {
    "COL_ID": "id3",
    "COL_INT_VAULE": 2020,
    "COL_BOOL_VALUE": true,
    "COL_FLOAT_VALUE": 1.41421,
    "COL_STRING_VAULE": "GIRL: Do you drink? PROGRAMMER: No. GIRL: Have Girlfriend? PROGRAMMER: No. GIRL: Then how do you enjoy life? PROGRAMMER: I am Programmer"
    }
]
''')

# create a nested list of the records' values
values = [list(x.values()) for x in test_json]
# print(values)

# get the column names
columns = [list(x.keys()) for x in test_json][0]

# value string for the SQL string
values_str = ""

# enumerate over the records' values
for i, record in enumerate(values):

    # declare empty list for values
    val_list = []
   
    # append each value to a new list of values
    for v, val in enumerate(record):
        if type(val) == str:
            val = "'{}'".format(val.replace("'", "''"))
        val_list += [ str(val) ]

    # put parenthesis around each record string
    values_str += "(" + ', '.join( val_list ) + "),\n"

# remove the last comma and end SQL with a semicolon
values_str = values_str[:-2] + ";"

# concatenate the SQL string
table_name = "json_data"
sql_string = "INSERT INTO %s (%s)\nVALUES\n%s" % (
    table_name,
    ', '.join(columns),
    values_str
)

print("\nSQL string:\n\n")
print(sql_string)

输出:


SQL string:


INSERT INTO json_data (COL_ID, COL_INT_VAULE, COL_BOOL_VALUE, COL_FLOAT_VALUE, COL_STRING_VAULE)
VALUES
('id1', 7, True, 3.14159, 'stackoverflow answer'),
('id2', 10, False, 2.71828, 'http://stackoverflow.com/'),
('id3', 2020, True, 1.41421, 'GIRL: Do you drink? PROGRAMMER: No. GIRL: Have Girlfriend? PROGRAMMER: No. GIRL: Then how do you enjoy life? PROGRAMMER: I am Programmer.');