所以我遵循这个tutorial,在我将Python连接到MySQL之后,我得到了200个响应代码,但是在postman上我看到了这个: { "错误":"%d格式:需要一个数字,而不是str" }
然后我在MySQL上检查我的表,没有任何反应,它仍然是空的。请帮帮我。
这是代码:
from flask import Flask
from flask_restful import Resource, Api, reqparse
from flaskext.mysql import MySQL
mysql = MySQL()
app = Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_PORT'] = '5002'
app.config['MYSQL_DATABASE_DB'] = 'itemlistdb'
mysql.init_app(app)
api = Api(app)
class CreateUser(Resource):
def post(self):
try:
parser = reqparse.RequestParser()
parser.add_argument('email', type=str, help='Email address to create user')
parser.add_argument('password', type=str, help='Password to create user')
args = parser.parse_args()
p_Username = args['email']
p_Password = args['password']
conn = mysql.connect()
cursor = mysql.get_db().cursor()
cursor.callproc('spCreateuser',(p_Username,p_Password))
data = cursor.fetchall()
if len(data) is 0:
conn.commit()
return {'statusCode':'200','Message': 'User creation success'}
else:
return {'StatusCode':'1000','Message': str(data[0])}
except Exception as e:
return {'error': str(e)}
class AuthenticateUser(Resource):
def post(self):
try:
parser = reqparse.RequestParser()
parser.add_argument('email', type=str, help='Email address for Authentication')
parser.add_argument('password', type=str, help='Password for Authentication')
args = parser.parse_args()
p_Username = args['email']
p_Password = args['password']
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_AuthenticateUser',(p_Username,))
data = cursor.fetchall()
if(len(data)>0):
if(str(data[0][2])==p_Password):
return {'status':200,'UserId':str(data[0][0])}
else:
return {'status':100,'message':'Authentication failure'}
except Exception as e:
return {'error': str(e)}
api.add_resource(CreateUser, '/CreateUser')
api.add_resource(AuthenticateUser, '/AuthenticateUser')
if __name__ == '__main__':
app.run(debug=True)
答案 0 :(得分:0)
我怀疑问题不是创建用户的调用,而是应用程序连接本身。
具体来说,我对此感到疑惑:
app.config['MYSQL_DATABASE_PORT'] = '5002'
服务器端口号通常是整数(这不是Python的东西,它是架构标准),所以当MySQL尝试连接时,它可能会做类似的事情:
'<rest-of-the-connection:%d' % app.config['MYSQL_DATABASE_PORT']
在这种情况下,app.config['MYSQL_DATABASE_PORT']
是str,而不是int。
尝试
app.config['MYSQL_DATABASE_PORT'] = 5002