My app.py code :
JavaScript
x
30
30
1
@app.route('/register' , methods = ['GET', 'POST'])
2
def register():
3
msg = ''
4
if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:
5
username = request.form['username']
6
password = request.form['password']
7
email = request.form['email']
8
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
9
cursor.execute('SELECT * FROM accounts WHERE username =%s', (username, ))
10
account = cursor.fetchone()
11
if account:
12
msg = "Account already exists !"
13
elif not re.match(r'[^@]+@[^@]+.[^@]+', email):
14
msg = 'Invalid email adress !'
15
16
elif not re.match(r'[A-Za-z0-9]+', username):
17
msg = 'Username must contain only characters and numbers !'
18
elif not username or not password or not email:
19
msg = 'Please fill the form !'
20
else:
21
cursor.execute('INSERT INTO accounts VALUES (NULL ,%s,%s,%s', (username, password, email, ))
22
mysql.connection.commit()
23
msg = 'You have successfully registered !'
24
elif request.method == 'POST':
25
msg = 'Please fill out the form !'
26
return render_template('register.html' , msg = msg)
27
28
if __name__ == '__main__':
29
app.run(debug = True)
30
This is the error I get :
JavaScript
1
23
23
1
Traceback (most recent call last):
2
File "D:Codespoofwebapp2envlibsite-packagesflaskapp.py", line 2548, in __call__
3
return self.wsgi_app(environ, start_response)
4
File "D:Codespoofwebapp2envlibsite-packagesflaskapp.py", line 2528, in wsgi_app
5
response = self.handle_exception(e)
6
File "D:Codespoofwebapp2envlibsite-packagesflaskapp.py", line 2525, in wsgi_app
7
response = self.full_dispatch_request()
8
File "D:Codespoofwebapp2envlibsite-packagesflaskapp.py", line 1822, in full_dispatch_request
9
rv = self.handle_user_exception(e)
10
File "D:Codespoofwebapp2envlibsite-packagesflaskapp.py", line 1820, in full_dispatch_request
11
rv = self.dispatch_request()
12
File "D:Codespoofwebapp2envlibsite-packagesflaskapp.py", line 1796, in dispatch_request
13
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
14
File "D:Codespoofwebapp2app.py", line 68, in register
15
cursor.execute('INSERT INTO accounts VALUES (NULL ,%s,%s,%s', (username, password, email, ))
16
File "D:Codespoofwebapp2envlibsite-packagesMySQLdbcursors.py", line 206, in execute
17
res = self._query(query)
18
File "D:Codespoofwebapp2envlibsite-packagesMySQLdbcursors.py", line 319, in _query
19
db.query(q)
20
File "D:Codespoofwebapp2envlibsite-packagesMySQLdbconnections.py", line 254, in query
21
_mysql.connection.query(self, query)
22
MySQLdb.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1")
23
I am using python 3.10.2 and flask 2.2.2 in my virtual environment. And I am using Visual Studio IDE. It seems like I am doing something wrong in the MYSQL query. can you guys tell me what am i doing wrong here.
Advertisement
Answer
You are missing a closing bracket in your cursor.execute()
statement and the last comma is not needed. Also, you can specify the column names.
Here is a solution with the query and values separated into variables for readability:
JavaScript
1
4
1
sql_query = 'INSERT INTO accounts(username, password, email) VALUES (%s, %s, %s)'
2
vals = (username, password, email)
3
cursor.execute(sql_query, vals)
4