运行此代码时我保留错误1318,我应该有4个参数:username
,email
,password
和location
。它正在上升4,但认为它只需要3个参数。数据库和Python代码如下。
的Python:
@app.route('/userSignUp',methods = ['POST'])
def userSignUp():
try:
#read values from signup form
_username = request.form['username']
_email = request.form['email']
_password = request.form['password']
_location = request.form['location']
#validate recieved values
if _username and _email and _password and _location:
cur = mysql.connection.cursor()
_hashed_password = generate_password_hash(_password)
cur.callproc('sp_createUser', (_username, _email, _hashed_password, _location))
data = cur.fetchall()
cur.close()
if len(data) is 0:
mysql.connection.commit()
return json.dumps({'success':'User created successfully!'})
else:
return json.dumps({'error':str(data[0])})
else:
return json.dumps({'html':'<span>Enter the required fields</span>'})
except Exception as e:
return json.dumps({'error':str(e)})
SQL:
# Create Database for ComicHub
CREATE DATABASE ComicHub;
# Create Table 'users' for ComicHub
CREATE TABLE `ComicHub`.`tbl_user` (
`user_id` BIGINT NULL AUTO_INCREMENT,
`user_username` VARCHAR(45) NULL,
`user_email` VARCHAR(45) NULL,
`user_password` VARCHAR(45) NULL,
`user_location` VARCHAR(66) NULL,
PRIMARY KEY (`user_id`)
);
# PROCEDURE for creating users from passed in data
USE `ComicHub`;
DELIMITER $$
CREATE PROCEDURE `sp_createUser` (
IN p_username VARCHAR(20),
IN p_email VARCHAR(20),
IN p_password VARCHAR(20),
IN P_location VARCHAR(20)
)
BEGIN
#check if user already exists
IF (select exists (select 1 from tbl_user where user_username = p_username) ) THEN
select 'Username Already Exists!';
ELSE
insert into tbl_user
(
user_username,
user_email,
user_password,
user_location
)
values
(
p_username,
p_email,
p_password,
p_location
);
END IF;
END$$
DELIMITER ;
答案 0 :(得分:0)
没关系,现在修复,我意识到我没有在MySQL Workbench中更新数据库。愚蠢我知道,但这是漫长的一天!