Merge branch 'main' of ssh://127.0.0.1:6020/home/git/srv/Principles_of_Database_System

This commit is contained in:
Jingfan Ke 2024-06-10 15:35:13 +08:00
commit b59880f301
4 changed files with 48 additions and 23 deletions

View File

@ -58,9 +58,20 @@ CREATE PROCEDURE RegisterPassenger(
OUT result_message VARCHAR(255)
)
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`)
VALUES (p_id, p_name, p_phone_number, p_password);
SET result_message = '注册成功';
COMMIT;
END;
//
@ -101,6 +112,14 @@ CREATE PROCEDURE ModifyPassengerInfo(
OUT result_message VARCHAR(255)
)
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
DELETE FROM passengers WHERE ID = p_id;
SET result_message = '删除账户成功';
@ -113,6 +132,8 @@ BEGIN
ELSE
SET result_message = '无效的修改类型';
END IF;
COMMIT;
END;
//

View File

@ -10,7 +10,7 @@ app.secret_key = os.environ.get('SECRET_KEY', 'OPTIONALSECRETKEY')
def get_db():
return pymysql.connect(
host='localhost', user='kejingfan',
password='xxxxxxxx', database='DBLab_7_1',
password='KJF2811879', database='DBLab_7_1',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
@ -34,33 +34,31 @@ def signup():
cursor = db.cursor()
try:
cursor.callproc('RegisterPassenger', (id, name, phone_number, password, "@result_message"))
cursor.fetchall()
cursor.callproc('RegisterPassenger', (id, name, phone_number, password, ""))
result = cursor.fetchall()
cursor.execute("SELECT @_RegisterPassenger_4;")
result_message = cursor.fetchone()['@_RegisterPassenger_4']
result_message = cursor.fetchone()
print(result_message)
flash(result_message)
if type(result_message) is dict:
flash(result_message['@_RegisterPassenger_4'])
else:
flash(result_message[1])
db.commit()
except pymysql.MySQLError as e:
db.rollback()
if e.args[0] == 1644: # SQLSTATE 45000 corresponds to error code 1644
flash("乘客已存在,无法重复注册")
else:
print(e)
flash("数据库异常,注册失败")
finally:
db.close()
return redirect(url_for('index'))
def verify_user(cursor: Cursor, id: str, password: str) -> str:
try:
cursor.callproc('VerifyUser', (id, password, "@verify_status"))
cursor.callproc('VerifyUser', (id, password, ""))
cursor.fetchall()
cursor.execute("SELECT @_VerifyUser_2;")
verify_status = cursor.fetchone()['@_VerifyUser_2']
except pymysql.MySQLError as e:
print(e)
if e.args[0] == 1644: # SQLSTATE 45000 corresponds to error code 1644
return "NO_USER"
return "DB_ERROR"
return verify_status
@ -78,11 +76,16 @@ class ModifyInfo:
self.command = modifyType2command[modifyType]
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):
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'))
def modify():
@ -105,6 +108,7 @@ def modify():
db.close()
return redirect(url_for('modify'))
elif verify_info == "DB_ERROR":
flash("数据库异常,验证失败")
db.close()
return redirect(url_for('modify'))
@ -116,11 +120,9 @@ def modify():
flash(modifyInfo.get_ok_message(cursor))
except pymysql.MySQLError as e:
db.rollback()
if e.args[0] == 1644: # SQLSTATE 45000 corresponds to error code 1644
flash("用户不存在,无法修改")
else:
print(e)
flash("数据库异常,修改失败")
print(e)
finally:
db.close()
return redirect(url_for('index'))

View File

@ -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