我一直在尝试连接到MySql数据库来创建用户帐户。我正在使用MySQL程序插入数据库。我一直在标题中得到错误。我正在使用mysqldb flask扩展这是我的Python代码。
from flask import Flask, render_template, json, request
from flask.ext.mysqldb import MySQL
from werkzeug import generate_password_hash, check_password_hash
mysql = MySQL()
app = Flask(__name__)
# MySQL configurations
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'database'
app.config['MYSQL_HOST'] = 'localhost'
mysql.init_app(app)
@app.route('/')
def main():
return render_template('index.html')
@app.route('/showSignUp')
def showSignUp():
return render_template('signup.html')
@app.route('/signUp', methods=['POST','GET'])
def signUp():
try:
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']
# validate the received values
if _name and _email and _password:
cursor = mysql.connection.cursor()
_hashed_password = generate_password_hash(_password)
cursor.callproc('sp_createUser',(_name,_email,_hashed_password))
data = cursor.fetchall()
if len(data) is 0:
cursor.execute()
return json.dump({'message':'User created successfully'})
else:
return json.dump({'error 1':str(data[0])})
else:
return json.dump({'html':'<span>Enter the required fields</span>'})
except Exception as e:
return json.dumps({'error 2':str(e)})
finally:
cursor.close()
if __name__ == '__main__':
app.run()
我的程序:
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_createUser`(
IN p_name VARCHAR(45),
IN p_username VARCHAR(45),
IN p_password VARCHAR(256)
)
BEGIN
if ( select exists (select 1 from tbl_user where user_username = p_username) ) THEN
select 'Username Exists !!';
ELSE
insert into tbl_user
(
user_name,
user_username,
user_password
)
values
(
p_name,
p_username,
p_password
);
END IF;
END