Inserting JSON into MySQL using Python

岁酱吖の 提交于 2019-11-28 17:59:15

use json.dumps(json_value) to convert your json object(python object) in a json string that you can insert in a text field in mysql

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

To expand on the other answers:

Basically you need make sure of two things:

  1. That you have room for the full amount of data that you want to insert in the field that you are trying to place it. Different database field types can fit different amounts of data. See: MySQL String Datatypes. You probably want the "TEXT" or "BLOB" types.

  2. That you are safely passing the data to database. Some ways of passing data can cause the database to "look" at the data and it will get confused if the data looks like SQL. It's also a security risk. See: SQL Injection

The solution for #1 is to check that the database is designed with correct field type.

The solution for #2 is use parameterized (bound) queries. For instance, instead of:

# 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 + ")")

Better, use:

# 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)

Hope this helps. If so, let me know. :-)

If you are still having a problem, then we will need to examine your syntax more closely.

You should be able to insert intyo a text or blob column easily

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

The error may be due to an overflow of the size of the field in which you try to insert your json. Without any code, it is hard to help you.

Have you considerate a no-sql database system such as couchdb, which is a document oriented database relying on json format?

You need to get a look at the actual SQL string, try something like this:

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)

I imagine you are going to find some stray quotes, brackets or parenthesis in there.

@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)

The most straightforward way to insert a python map into a MySQL JSON field...

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

sql = "INSERT INTO your_table (json_column_name) VALUES (%s)"
cursor.execute( sql, (json.dumps(python_map),) )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!