如何在 python 3 flask-restful 中使用数组执行插入语句

How to perform insert statement using arrays in python 3 flask-restful

我想使用数组列表对 json 进行多个输入。下面的代码仅显示单个 post。

parser = reqparse.RequestParser()
parser.add_argument('tblskuid', type = str)
parser.add_argument('tbluserid', type = str)
args = parser.parse_args()

tblskuid = args['tblskuid']
tbluserid = args['tbluserid']

tbl_bo = [
         (tblskuid, tbluserid, )
            ]

conn = psycopg2.connect(database='mobiletracker', user='', host='', password='')
cursor = conn.cursor()
args_str = ','.join(['%s'] * len(tbl_bo))
cursor.execute(cursor.mogrify('INSERT INTO tbl_bad_orders_product(tblskuid, tbluserid) VALUES {}'.format(args_str), tbl_bo).decode('utf8'))
conn.commit()
cursor.close()

我想要的是像这样在数组中插入一个数据列表。

tbl_bo = [
      {
        "tbl_skuid":"1",
        "tbluserid":"1",

      },
      {
        "tbl_skuid":"message",
        "tbluserid":"employeeid",

      }
    ] 

以及我应该如何使用 postman 来执行 posting 响应?任何帮助将不胜感激。提前致谢。

我要回答我自己的问题,因为我自己解决了。我们将使用游标函数使用 json 响应。我不想使用 sqlAlchemy,因为它很复杂并且在大数据 posting 中存在速度问题。我认为 RequestParser() 不是使用数组的多个 post 的有效方法。现在是代码。

class Logs_api(Resource):
    @jwt_required
    def post(self):
        try: 
          conn = None
          #getting the json request of post data 
          json_dict = request.get_json(force=True, silent=True)

          #count the length of json dictionary
          x = len(json_dict)


          tbl_logs = []
          for i in chain(range(0, x)):

            tbl_logs.append((json_dict[i]['tbluserid'],json_dict[i]['event'],json_dict[i]['current_longitude'],json_dict[i]['current_latitude'],json_dict[i]['end_longitude'],json_dict[i]['end_latitude'],json_dict[i]['gps_accuracy'],json_dict[i]['gps_provider'],json_dict[i]['battery'],json_dict[i]['datetime_log'], ))

          conn = psycopg2.connect(database='yourdb', user='dbuser', host='dbhost', password='dbpass', options='-c statement_timeout=1000')  
          cursor = conn.cursor()
          args_str = ','.join(['%s'] * len(tbl_logs))
        #   print(cursor.mogrify('INSERT INTO tbl_location_logs(tbluserid, event, current_longitude, current_latitude, end_longitude, end_latitude, gps_accuracy, gps_provider, battery, datetime_log) VALUES {}'.format(args_str), tbl_logs).decode('utf8'))
          cursor.execute(cursor.mogrify('INSERT INTO tbl_location_logs(tbluserid, event, current_longitude, current_latitude, end_longitude, end_latitude, gps_accuracy, gps_provider, battery, datetime_log) VALUES {}'.format(args_str), tbl_logs).decode('utf8'))
          conn.commit()
          cursor.close()

          return {'status' : 'success', 'message' : 'success'}
        except Exception as e:
            x = str(e)
            x.replace('\n', '')

            return {'status' : 'failed', 'message' : str(x)}
        finally:
            if conn is not None:
                conn.close()

在 postman 中,使用 raw 而不是 x-www-form-urlencoded 并在那里输入你的 json。这是我的示例格式。

[ 
  {
        "tbluserid": "2",
        "event": "sample",
        "current_longitude": "20",
        "current_latitude": "200",
        "end_longitude": "20",
        "end_latitude": "200",
        "gps_accuracy": "200",
        "gps_provider": "google maps pangett",
        "battery": "200",
        "datetime_log": "2018-02-02 23:00:00"
    }
]

借助上面的代码结构,我 post 在短短 1.2 秒内编辑了总共 5800+ 条数据,证明是下面的 2 张图片: