POST 得到的数据如果已经存在于数据库 Genre 表里,应该显示“该数据已存在于数据库中”,但代码出现问题,只会直接显示“未能将指定内容加入数据库”
@app.route('/add/genre', methods = ['POST'])
def add_genre():
if request.method == 'POST':
genre = request.form['genre']
conn = sqlite3.connect("Mydatabase.db")
cur = conn.cursor()
cur.execute("SELECT Name FROM Genre")
genreName = cur.fetchall()
string = "成功添加 " + genre + " 到数据库."
prompt = {string}
error = ""
if genre in genreName: // ←
prompt = {"该数据已存在于数据库中"}
error = "exist"
else:
with sqlite3.connect("Mydatabase.db") as conn:
try:
cur = conn.cursor()
cur.execute("INSERT INTO Genre (Name) VALUES (?)", ('genre'))
conn.commit()
except:
prompt = {"未能将指定内容加入数据库"}
error = "exist"
return render_template('add_genre.html', messages=prompt, error=error)