Python Flask MySQL,UPDATE 查询有问题
Python Flask MySQL, problem with UPDATE query
我刚开始使用 Python 进行一个新项目,我想就一个非常恼人的问题寻求帮助。
环境[Python,烧瓶,MySQL]
我目前正在开发一个 Web 应用程序(登录、用户信息管理、调查表)
当用户第一次在应用程序上注册时,用户名、密码、邮件地址被插入到数据库中。
另外,由于我需要记录对调查表的每一个答案,我自动将 0 值分配给 Q001 和 Q002(问题)。
登录后,用户将开始回答调查,我只需要那些答案,以替换相应行内的值。
我试图使用 UPDATE SET WHERE 查询,但我不断收到“格式字符串的参数不足”错误。
这是python代码
poll_data = {
'question1' : 'Are you a donkey?',
'question2' : 'Are you a pig?',
}
@app.route('/flaskProject/register', methods=['GET', 'POST'])
def register():
# Output message if something goes wrong...
msg = ''
# Check if "username", "password" and "email" POST requests exist (user submitted form)
if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:
# Create variables for easy access
username = request.form['username']
password = request.form['password']
email = request.form['email']
Q001 = 0
Q002 = 0
# Check if account exists using MySQL
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE username = %s', (username,))
account = cursor.fetchone()
# If account exists show error and validation checks
if account:
msg = 'Account already exists!'
elif not username or not password or not email:
msg = 'Please fill out the form!'
else:
# Account doesn't exists and the form data is valid, now insert new account into accounts table
cursor.execute('INSERT INTO accounts VALUES (NULL, %s, %s, %s, %s, %s)', (username, password, email, Q001, Q002, ))
mysql.connection.commit()
msg = 'You have successfully registered!'
elif request.method == 'POST':
# Form is empty... (no POST data)
msg = 'Please fill out the form!'
# Show registration form with message (if any)
return render_template('register.html', msg=msg)
@app.route("/flaskProject/questionnaire1", methods=['GET', 'POST'])
def questionnaire1():
# Check if user is loggedin
if request.method == 'POST' and 'loggedin' in session:
# Create variables for easy access
Q001 = request.form['Q001']
Q002 = request.form['Q002']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("UPDATE accounts SET Q001 = %s, Q002 = %s WHERE id = %s", (session['id'],))
mysql.connection.commit()
# Redirect to survey
return render_template('questionnaire1.html', data=poll_data)
下面是我的BD
CREATE DATABASE IF NOT EXISTS `flaskProject` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `flaskProject`;
CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
'Q001' varchar(50) NOT NULL,
'Q002' varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `accounts` (`id`, `username`, `password`, `email`, 'Q001', 'Q002') VALUES (1, 'test', 'test', 'test@test.com', '0', '0');
最后是调查页面html
<form action="{{ url_for('questionnaire1') }}" method="post" autocomplete="off">
<table style="width:100%">
<tr>
<td>{{ data.question1 }}</td>
<td style="text-align:right">
<label for="Q001"></label>
<input type="text" name="Q001" placeholder="Q001" id="Q001" required></td>
</tr>
<tr>
<td>{{ data.question2 }}</td>
<td style="text-align:right">
<label for="Q002"></label>
<input type="text" name="Q002" placeholder="Q002" id="Q002" required></td>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<br>
<div class="msg">{{ msg }}</div>
<input type="submit" value="Next">
</form>
对于每个占位符 - %s
- 在你的查询中,你需要在元组中提供一个变量,它是 .execute()
.
的第二个参数
您拥有所需的变量,只是您尚未提供它们。
将 UPDATE
查询行更改为:
cursor.execute("UPDATE accounts SET Q001 = %s, Q002 = %s WHERE id = %s",
(Q001, Q002, session['id'],))
...你应该回到正轨。
我刚开始使用 Python 进行一个新项目,我想就一个非常恼人的问题寻求帮助。
环境[Python,烧瓶,MySQL] 我目前正在开发一个 Web 应用程序(登录、用户信息管理、调查表)
当用户第一次在应用程序上注册时,用户名、密码、邮件地址被插入到数据库中。 另外,由于我需要记录对调查表的每一个答案,我自动将 0 值分配给 Q001 和 Q002(问题)。
登录后,用户将开始回答调查,我只需要那些答案,以替换相应行内的值。 我试图使用 UPDATE SET WHERE 查询,但我不断收到“格式字符串的参数不足”错误。 这是python代码
poll_data = {
'question1' : 'Are you a donkey?',
'question2' : 'Are you a pig?',
}
@app.route('/flaskProject/register', methods=['GET', 'POST'])
def register():
# Output message if something goes wrong...
msg = ''
# Check if "username", "password" and "email" POST requests exist (user submitted form)
if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:
# Create variables for easy access
username = request.form['username']
password = request.form['password']
email = request.form['email']
Q001 = 0
Q002 = 0
# Check if account exists using MySQL
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE username = %s', (username,))
account = cursor.fetchone()
# If account exists show error and validation checks
if account:
msg = 'Account already exists!'
elif not username or not password or not email:
msg = 'Please fill out the form!'
else:
# Account doesn't exists and the form data is valid, now insert new account into accounts table
cursor.execute('INSERT INTO accounts VALUES (NULL, %s, %s, %s, %s, %s)', (username, password, email, Q001, Q002, ))
mysql.connection.commit()
msg = 'You have successfully registered!'
elif request.method == 'POST':
# Form is empty... (no POST data)
msg = 'Please fill out the form!'
# Show registration form with message (if any)
return render_template('register.html', msg=msg)
@app.route("/flaskProject/questionnaire1", methods=['GET', 'POST'])
def questionnaire1():
# Check if user is loggedin
if request.method == 'POST' and 'loggedin' in session:
# Create variables for easy access
Q001 = request.form['Q001']
Q002 = request.form['Q002']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("UPDATE accounts SET Q001 = %s, Q002 = %s WHERE id = %s", (session['id'],))
mysql.connection.commit()
# Redirect to survey
return render_template('questionnaire1.html', data=poll_data)
下面是我的BD
CREATE DATABASE IF NOT EXISTS `flaskProject` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `flaskProject`;
CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
'Q001' varchar(50) NOT NULL,
'Q002' varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `accounts` (`id`, `username`, `password`, `email`, 'Q001', 'Q002') VALUES (1, 'test', 'test', 'test@test.com', '0', '0');
最后是调查页面html
<form action="{{ url_for('questionnaire1') }}" method="post" autocomplete="off">
<table style="width:100%">
<tr>
<td>{{ data.question1 }}</td>
<td style="text-align:right">
<label for="Q001"></label>
<input type="text" name="Q001" placeholder="Q001" id="Q001" required></td>
</tr>
<tr>
<td>{{ data.question2 }}</td>
<td style="text-align:right">
<label for="Q002"></label>
<input type="text" name="Q002" placeholder="Q002" id="Q002" required></td>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<br>
<div class="msg">{{ msg }}</div>
<input type="submit" value="Next">
</form>
对于每个占位符 - %s
- 在你的查询中,你需要在元组中提供一个变量,它是 .execute()
.
您拥有所需的变量,只是您尚未提供它们。
将 UPDATE
查询行更改为:
cursor.execute("UPDATE accounts SET Q001 = %s, Q002 = %s WHERE id = %s",
(Q001, Q002, session['id'],))
...你应该回到正轨。