如何在flask应用程序中提交存储过程

时间:2017-12-03 10:09:18

标签: python sql-server flask flask-sqlalchemy

我正在尝试调用ms sql过程的烧瓶应用程序,当我调用数据时没有提交。它的插入和回滚,因为我理解因为增量在表中发生变化。怎么提交?

# config.py
from flask import Flask
from flask_restful import Api
from sqlalchemy import create_engine
from sqlalchemy.orm import create_session

connection = create_engine(
    'mssql+pyodbc://......
)

session = create_session(bind=connection, autocommit=True)

app = Flask(__name__)
api = Api(app)

# models.py

class user_registration(Resource):
   def post(self):
       username = request.json['username']
       name = request.json['name']
       surname = request.json['surname']
       password = request.json['password']
       is_admin = request.json['is_admin']
       session.begin()
       query = connection.execute('exec [dbo].[user_registration] ?,?,?,?,?', username, name, surname, password,
                                  is_admin)
       result = {'data': [dict(zip(tuple(query.keys()), i)) for i in query.cursor]}
       session.commit()
       return jsonify(result)

# python post script

import requests

r = requests.post(url='http://127.0.0.1:8000/user_registration', json={
   "username": "testo",
   "name": "test",
   "surname": "test",
   "password": "test3",
   "is_admin": "0"
})
print(r.json())

0 个答案:

没有答案