Python迭代字典列表并保存到db

时间:2017-05-30 14:26:14

标签: python mysql python-3.x

我是python的新手,并尝试将python中的原始发布数据保存到mysql中。

我想迭代发布的json中的每个元素,并将所有数据保存到DB。

json对象列表:(每个11列有30个对象)

[
 {
   "col1":7878,
   "col2":"c004979d3969a86a8fdcda2f92eb39e3",
   "col3":"b000yht23",
    ...
   "col11":2

 },
 {
   "col1":7878,
   "col2":"c004979d3969a86a8fdcda2f92eb39e3",
   "col3":"b000yht23"
    ...
   "col11":43 
 },
 #upto 30 objects
 ....
]

' json_test'表desc:

  

CREATE TABLE json_test

`col1` varchar(250) NOT NULL,

`col2` varchar(250) NOT NULL,

`col3` varchar(250) NOT NULL,

`col4` varchar(250) NOT NULL,

`col5` varchar(250) NOT NULL,

`col6` varchar(250) NOT NULL,

`col7` varchar(250) NOT NULL,

`col8` varchar(250) NOT NULL,

`col9` varchar(250) NOT NULL,

`col10` varchar(250) NOT NULL,

`col11` varchar(200) NOT NULL
     

)ENGINE = InnoDB DEFAULT CHARSET = latin1;

更新以将数据保存到数据库:

我的py代码如下:

from flask import Flask, abort, request 
import json
import pymysql

app = Flask(__name__)


@app.route('/foo', methods=['GET','POST']) 
def foo():
    jsonobject=request.json
    if not jsonobject:
        abort(400)

# load- converts JSON source text to a Python value
#readable_json=json.dumps(jsonobject)

#UPDATED with column_names
k=0
for i in jsonobject:
    # Connect to the database
    conn = pymysql.connect(host='10.20.3.4', port=3306, user='root', passwd='', db='python_db')


    try:
        with conn.cursor() as cursor:
            column_names = ['col1','col2','col3',...'col11']
            column_names_str = ', '.join(column_names)
            binds_str = ', '.join('%s' for _ in range(len(column_names)))
            sql=("INSERT INTO `json_test` ({column_names})" \
                  " VALUES({binds})"
            .format(column_names=column_names_str,binds=binds_str))   
            for data_dict in jsonobject:
            values = [data_dict[column_name]
                      for column_name in column_names]

            cursor.execute(sql, values)
            print("Insert successfull!")
            #UPDATED 
            k+=1
            conn.commit()

    finally:
        conn.close()


return "Insert successful"
#return json.dumps(jsonobject)


if __name__ == '__main__':
    app.run(host='10.22.1.168',debug=True,port=7845)

更新的代码结果:

只有最后一条记录似乎正在插入

2 个答案:

答案 0 :(得分:1)

替换这个烂摊子

#UPDATED with column_names
k=0
for i in jsonobject:
    # Connect to the database
    conn = pymysql.connect(host='10.20.3.4', port=3306, user='root', passwd='', db='python_db')


    try:
        with conn.cursor() as cursor:
            column_names = ['col1','col2','col3',...'col11']
            column_names_str = ', '.join(column_names)
            binds_str = ', '.join('%s' for _ in range(len(column_names)))
            sql=("INSERT INTO `json_test` ({column_names})" \
                  " VALUES({binds})"
            .format(column_names=column_names_str,binds=binds_str))   
            for data_dict in jsonobject:
            values = [data_dict[column_name]
                      for column_name in column_names]

            cursor.execute(sql, values)
            print("Insert successfull!")
            #UPDATED 
            k+=1
            conn.commit()

    finally:
        conn.close()


return "Insert successful"

try:
    with conn.cursor() as cursor:
        columns_names = ['col1', 'col2', 'col3', 'col4', 'col5', 'col6',
                         'col7', 'col8', 'col9', 'col10', 'col11']
        columns_names_str = ', '.join(columns_names)
        binds_str = ', '.join('%s' for _ in range(len(columns_names)))
        for data_dict in jsonobject:
            sql = ("INSERT INTO json_test ({columns_names}) "
                   "VALUES ({binds})"
                   .format(columns_names=columns_names_str,
                           binds=binds_str))
            values = [data_dict[column_name]
                      for column_name in columns_names]
            cursor.execute(sql, values)
        print("Insert successfull!")
        conn.commit()
finally:
    conn.close()

求和

  • k对象是多余的,
  • 名称i也不清楚,让我觉得它不是某种索引:它是dict对象,
  • 我们不需要为jsonobject中的每个对象创建连接,因为这是一项昂贵的操作,
  • 我们也不需要在每次迭代中创建sql对象(它保持不变),
  • list / tuple中存储列名称将使我们免于编写两次:在查询和值提取中。
  • 创建绑定str

    %s, %s, ...
    
    当我们错过/添加太多绑定别名时,

    动态地根据列数保存我们的错误

答案 1 :(得分:0)

json.dumps执行您所声称的相反;它将Python对象转换为字符串。

request.json的结果已经是Python数据结构。你不需要用它做任何其他事情。