如何将多行数据插入表中以维护 1NF?现在我有一个 html 表单,可以创建一个组,您可以在其中添加/邀请组成员加入。您可以添加/邀请多个或一个:组成员的输入可能因用户而异。我想获取用户输入并将其添加到维护 1NF 的表中。我正在考虑在 MySQL 中创建一个存储过程,并在我拥有值列表并进行“i”插入调用后调用它(i 是用户邀请的成员数)。
我的 App.route 用于创建团队:现在我正在将数据插入 tb_team 表,接下来我想用用户和关联的 team_id 填充 team_members 表
@app.route('/create-team/', methods=['GET', 'POST'])
@login_required
def create_team():
msg = ''
if request.method == 'POST':
# Create variables for easy access
team_name = request.form['team_name']
invite = request.form['invite']
team_desc = request.form['content']
if not team_name:
msg='Please enter a group name'
return render_template('create_team.html', msg=msg, team_name=team_name, invite=invite, content=team_desc)
if not invite:
msg = 'Please invite users to the team'
return render_template('create_team.html', msg=msg, team_name=team_name, invite=invite, content=team_desc)
if not invite and not team_name:
msg = 'Please fill out the form'
return render_template('create_team.html', msg=msg)
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT user_id from tb_user WHERE user_name = %s', (invite,))
invite_id = cursor.fetchone()
invite_id_final = invite_id['user_id']
# Check if team_name exists in tb_team table
cursor.execute('SELECT * FROM tb_team WHERE team_name = %s', (team_name,))
name = cursor.fetchone()
这是我的数据库的设计以及我最终想要实现的目标:
这里有虚拟数据:
编写存储过程是否是插入这些组成员的正确方法?
12345678_0001
相关分类