Python 我能';不能与SQL通信
我尝试连接MySQL,没有显示添加用户的成功状态,出现如下错误:Python 我能';不能与SQL通信,python,mysql,flask,Python,Mysql,Flask,我尝试连接MySQL,没有显示添加用户的成功状态,出现如下错误: {"message": "The method is not allowed for the requested URL."} 我不明白它说的是什么“方法”…我和邮递员一起做的帖子?我们将非常感谢您的帮助 所以我修改了我的代码,将MySQL()放在POST方法中,仍然会得到相同的错误 以下是我的“api.py”代码: 我使用一个存储过程在db中写入值,该过程是spCreateUser,下面是它的代码: BEGIN if ( s
{"message": "The method is not allowed for the requested URL."}
我不明白它说的是什么“方法”…我和邮递员一起做的帖子?我们将非常感谢您的帮助
所以我修改了我的代码,将MySQL()放在POST方法中,仍然会得到相同的错误
以下是我的“api.py”代码:
我使用一个存储过程在db中写入值,该过程是spCreateUser,下面是它的代码:
BEGIN
if ( select exists (select 1 from tblUser where UserName = p_username) )
THEN
select 'Username Exists !!';
ELSE
insert into tblUser(
UserName,
Password
)
values
(
p_Username,
p_Password
);
END IF;
结束
如果使用不受支持的HTTP方法调用资源,API将返回状态为405 method Not Allowed的响应
我猜您试图在浏览器上查看您的资源,因此出现此错误。若要在浏览器上查看资源,它必须支持httpget
方法
class CreateUser(Resource):
def post(self):
cur.execute("INSERT INTO table VALUES(?,?)",(email,password))
def get(self):
#...
还有一件事,
QueryDb
方法本身不会调用,您必须在一个http方法中调用它。好的,我解决了所有问题……可能对其他人有用:
因此,首先我将函数名加上“post”而不是“post”
然后我得到了以下信息:
“在处理第一个请求后调用了安装函数”
解决方法是禁用调试模式
下面是代码:
from flask import Flask
from flask_restful import Resource, Api, reqparse
from flaskext.mysql import MySQL
app = Flask(__name__)
api = Api(app)
class CreateUser(Resource):
def post(self):
try:
# Parse the arguments
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()
_userEmail = args['email']
_userPassword = args['password']
#return {'Email': args['email'], 'Password': args['password']}
mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'itemlistdb'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('spCreateUser',(_userEmail,_userPassword))
data = cursor.fetchall()
if len(data) is 0:
conn.commit()
return {'StatusCode':'200','Message': 'User creation success'}
cursor.close()
conn.close()
else:
return {'StatusCode':'1000','Message': str(data[0])}
cursor.close()
conn.close()
except Exception as e:
return {'error': str(e)}
api.add_resource(CreateUser, '/CreateUser')
if __name__ == '__main__':
app.run(debug=False)
如果像这样编辑代码,则无法使conn.commit()生效。。。。我需要更改光标。替换:cursor=mysql.get_db().cursor()为:conn=mysql.connect()cursor=conn.cursor()我只是想用POSTMAN在db中发布一个用户,但我做不到……然后在
post
方法中运行sql。我在Answer中编写了一个示例,但我的存储过程应该在我向POSTMAN发布电子邮件和密码时完成这项工作。。。。
from flask import Flask
from flask_restful import Resource, Api, reqparse
from flaskext.mysql import MySQL
app = Flask(__name__)
api = Api(app)
class CreateUser(Resource):
def post(self):
try:
# Parse the arguments
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()
_userEmail = args['email']
_userPassword = args['password']
#return {'Email': args['email'], 'Password': args['password']}
mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'itemlistdb'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('spCreateUser',(_userEmail,_userPassword))
data = cursor.fetchall()
if len(data) is 0:
conn.commit()
return {'StatusCode':'200','Message': 'User creation success'}
cursor.close()
conn.close()
else:
return {'StatusCode':'1000','Message': str(data[0])}
cursor.close()
conn.close()
except Exception as e:
return {'error': str(e)}
api.add_resource(CreateUser, '/CreateUser')
if __name__ == '__main__':
app.run(debug=False)