修改实验7-1的代码
This commit is contained in:
parent
e0c6827b2e
commit
40aa1c0ec8
Binary file not shown.
@ -58,9 +58,20 @@ CREATE PROCEDURE RegisterPassenger(
|
|||||||
OUT result_message VARCHAR(255)
|
OUT result_message VARCHAR(255)
|
||||||
)
|
)
|
||||||
BEGIN
|
BEGIN
|
||||||
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
||||||
|
BEGIN
|
||||||
|
ROLLBACK;
|
||||||
|
GET DIAGNOSTICS CONDITION 1 result_message = MESSAGE_TEXT;
|
||||||
|
END;
|
||||||
|
|
||||||
|
START TRANSACTION;
|
||||||
|
|
||||||
INSERT INTO passengers (ID, `Name`, Phone_number, `Password`)
|
INSERT INTO passengers (ID, `Name`, Phone_number, `Password`)
|
||||||
VALUES (p_id, p_name, p_phone_number, p_password);
|
VALUES (p_id, p_name, p_phone_number, p_password);
|
||||||
|
|
||||||
SET result_message = '注册成功';
|
SET result_message = '注册成功';
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
END;
|
END;
|
||||||
//
|
//
|
||||||
|
|
||||||
@ -101,6 +112,14 @@ CREATE PROCEDURE ModifyPassengerInfo(
|
|||||||
OUT result_message VARCHAR(255)
|
OUT result_message VARCHAR(255)
|
||||||
)
|
)
|
||||||
BEGIN
|
BEGIN
|
||||||
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
||||||
|
BEGIN
|
||||||
|
ROLLBACK;
|
||||||
|
GET DIAGNOSTICS CONDITION 1 result_message = MESSAGE_TEXT;
|
||||||
|
END;
|
||||||
|
|
||||||
|
START TRANSACTION;
|
||||||
|
|
||||||
IF p_modify_type = 'delete account' THEN
|
IF p_modify_type = 'delete account' THEN
|
||||||
DELETE FROM passengers WHERE ID = p_id;
|
DELETE FROM passengers WHERE ID = p_id;
|
||||||
SET result_message = '删除账户成功';
|
SET result_message = '删除账户成功';
|
||||||
@ -113,6 +132,8 @@ BEGIN
|
|||||||
ELSE
|
ELSE
|
||||||
SET result_message = '无效的修改类型';
|
SET result_message = '无效的修改类型';
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
END;
|
END;
|
||||||
//
|
//
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ app.secret_key = os.environ.get('SECRET_KEY', 'OPTIONALSECRETKEY')
|
|||||||
def get_db():
|
def get_db():
|
||||||
return pymysql.connect(
|
return pymysql.connect(
|
||||||
host='localhost', user='kejingfan',
|
host='localhost', user='kejingfan',
|
||||||
password='xxxxxxxx', database='DBLab_7_1',
|
password='KJF2811879', database='DBLab_7_1',
|
||||||
charset='utf8mb4',
|
charset='utf8mb4',
|
||||||
cursorclass=pymysql.cursors.DictCursor
|
cursorclass=pymysql.cursors.DictCursor
|
||||||
)
|
)
|
||||||
@ -34,33 +34,31 @@ def signup():
|
|||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cursor.callproc('RegisterPassenger', (id, name, phone_number, password, "@result_message"))
|
cursor.callproc('RegisterPassenger', (id, name, phone_number, password, ""))
|
||||||
cursor.fetchall()
|
result = cursor.fetchall()
|
||||||
cursor.execute("SELECT @_RegisterPassenger_4;")
|
cursor.execute("SELECT @_RegisterPassenger_4;")
|
||||||
result_message = cursor.fetchone()['@_RegisterPassenger_4']
|
result_message = cursor.fetchone()
|
||||||
print(result_message)
|
print(result_message)
|
||||||
flash(result_message)
|
if type(result_message) is dict:
|
||||||
|
flash(result_message['@_RegisterPassenger_4'])
|
||||||
|
else:
|
||||||
|
flash(result_message[1])
|
||||||
db.commit()
|
db.commit()
|
||||||
except pymysql.MySQLError as e:
|
except pymysql.MySQLError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
if e.args[0] == 1644: # SQLSTATE 45000 corresponds to error code 1644
|
print(e)
|
||||||
flash("乘客已存在,无法重复注册")
|
finally:
|
||||||
else:
|
db.close()
|
||||||
print(e)
|
|
||||||
flash("数据库异常,注册失败")
|
|
||||||
db.close()
|
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
def verify_user(cursor: Cursor, id: str, password: str) -> str:
|
def verify_user(cursor: Cursor, id: str, password: str) -> str:
|
||||||
try:
|
try:
|
||||||
cursor.callproc('VerifyUser', (id, password, "@verify_status"))
|
cursor.callproc('VerifyUser', (id, password, ""))
|
||||||
cursor.fetchall()
|
cursor.fetchall()
|
||||||
cursor.execute("SELECT @_VerifyUser_2;")
|
cursor.execute("SELECT @_VerifyUser_2;")
|
||||||
verify_status = cursor.fetchone()['@_VerifyUser_2']
|
verify_status = cursor.fetchone()['@_VerifyUser_2']
|
||||||
except pymysql.MySQLError as e:
|
except pymysql.MySQLError as e:
|
||||||
print(e)
|
print(e)
|
||||||
if e.args[0] == 1644: # SQLSTATE 45000 corresponds to error code 1644
|
|
||||||
return "NO_USER"
|
|
||||||
return "DB_ERROR"
|
return "DB_ERROR"
|
||||||
return verify_status
|
return verify_status
|
||||||
|
|
||||||
@ -78,11 +76,16 @@ class ModifyInfo:
|
|||||||
self.command = modifyType2command[modifyType]
|
self.command = modifyType2command[modifyType]
|
||||||
|
|
||||||
def get_args(self):
|
def get_args(self):
|
||||||
return (self.id, self.command, self.new_password, self.phone_number, "@result_message")
|
return (self.id, self.command, self.new_password, self.phone_number, "")
|
||||||
|
|
||||||
def get_ok_message(self, cursor):
|
def get_ok_message(self, cursor):
|
||||||
cursor.execute("SELECT @_ModifyPassengerInfo_4;")
|
cursor.execute("SELECT @_ModifyPassengerInfo_4;")
|
||||||
return cursor.fetchone()['@_ModifyPassengerInfo_4']
|
result_message = cursor.fetchone()
|
||||||
|
print(result_message)
|
||||||
|
if type(result_message) is dict:
|
||||||
|
return result_message['@_ModifyPassengerInfo_4']
|
||||||
|
else:
|
||||||
|
return result_message[1]
|
||||||
|
|
||||||
@app.route("/modify.html", methods=('GET', 'POST'))
|
@app.route("/modify.html", methods=('GET', 'POST'))
|
||||||
def modify():
|
def modify():
|
||||||
@ -105,6 +108,7 @@ def modify():
|
|||||||
db.close()
|
db.close()
|
||||||
return redirect(url_for('modify'))
|
return redirect(url_for('modify'))
|
||||||
elif verify_info == "DB_ERROR":
|
elif verify_info == "DB_ERROR":
|
||||||
|
flash("数据库异常,验证失败")
|
||||||
db.close()
|
db.close()
|
||||||
return redirect(url_for('modify'))
|
return redirect(url_for('modify'))
|
||||||
|
|
||||||
@ -116,12 +120,10 @@ def modify():
|
|||||||
flash(modifyInfo.get_ok_message(cursor))
|
flash(modifyInfo.get_ok_message(cursor))
|
||||||
except pymysql.MySQLError as e:
|
except pymysql.MySQLError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
if e.args[0] == 1644: # SQLSTATE 45000 corresponds to error code 1644
|
flash("数据库异常,修改失败")
|
||||||
flash("用户不存在,无法修改")
|
print(e)
|
||||||
else:
|
finally:
|
||||||
print(e)
|
db.close()
|
||||||
flash("数据库异常,修改失败")
|
|
||||||
db.close()
|
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -1 +1,3 @@
|
|||||||
SECRET_KEY='ILOVEDATABASETECH' FLASK_APP=main FLASK_ENV=development flask run --host 0.0.0.0 --port 5000
|
SECRET_KEY='ILOVEDATABASETECH' FLASK_APP=main FLASK_ENV=development flask run \
|
||||||
|
--host 0.0.0.0 \
|
||||||
|
--port 8888 # default 5000
|
Loading…
x
Reference in New Issue
Block a user