Inserting JSON into MySQL using Python

前端 未结 9 883
我在风中等你
我在风中等你 2020-12-08 03:50

I have a JSON object in Python. I am Using Python DB-API and SimpleJson. I am trying to insert the json into a MySQL table.

At moment am getting errors and I believ

相关标签:
9条回答
  • 2020-12-08 03:57

    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

    0 讨论(0)
  • 2020-12-08 03:57

    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?

    0 讨论(0)
  • 2020-12-08 03:58

    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.

    0 讨论(0)
  • 2020-12-08 04:01

    Here's a quick tip, if you want to write some inline code, say for a small json value, without import json. You can escape quotes in SQL by a double quoting, i.e. use '' or "", to enter ' or ".

    Sample Python code (not tested):

    q = 'INSERT INTO `table`(`db_col`) VALUES ("{k:""some data"";}")'
    db_connector.execute(q)
    
    0 讨论(0)
  • 2020-12-08 04:03
    @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)
    
    0 讨论(0)
  • 2020-12-08 04:12

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

    db.execute("INSERT INTO json_col VALUES %s", json_value)
    
    0 讨论(0)
提交回复
热议问题