I just started using Python for a new project and I would like some help on a very irritating issue. Environment[Python, Flask, MySQL] I am currently developing a Web Application (login, user information management, survey form) When the user first register on the application, username, password, mail address are inserted into the DB. Also, since I need to record every answer given to the survey form, I automatically assign a 0 value to Q001 and Q002 (questions). Once loggedin, the user will start answering the survey and I want those answers, and those answers only, to replace the value inside the corresponding row. I was trying to use the UPDATE SET WHERE query but I keep getting a "not enough arguments for format string" error. This is the python code 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)below is my 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', '[email protected]', '0', '0');and finally the html of the survey page <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> (责任编辑:) |