Reputation:
I am using:
To connect to my database, I am using the following code snippet, from Using SQLite 3 with Flask document:
import sqlite3
from flask import g
DATABASE = '/path/to/database.db'
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
and after the following piece of code is executed:
cursor = get_db().cursor()
hashed_password = werkzeug.security.generate_password_hash(request.form['password'], method='pbkdf2:sha512', salt_length=25)
activation_token = werkzeug.security.generate_password_hash(request.form['login'], method='pbkdf2:sha512', salt_length=25)
cursor.execute('INSERT INTO Users (activation_token, login, email, password, name) VALUES (?, ?, ?, ?, ?)' (activation_token, request.form['login'], request.form['email'], hashed_password, request.form['name']))
cursor.close()
close_connection(None)
I am receiving:
File "path/to/my_file.py", line 33, in register
cursor.execute('INSERT INTO Users (activation_token, login, email, password, name) VALUES (?, ?, ?, ?, ?)' (activation_token, request.form['login'], request.form['email'], hashed_password, request.form['name']))
TypeError: 'str' object is not callable
What is the mistake here?
I sense it should be some trivial thing, though I wasn't able to pin it down. What adds to the confusion — SELECT
queries in other places of my code are working just fine.
Upvotes: 0
Views: 387
Reputation:
Figured it out myself, while composing the question =).
Edited the title of the question accordingly.
The culprit turned out to be missing comma, between arguments of cursor.execute()
(pretty trivial, just like I have anticipated).
So, line:
cursor.execute('INSERT INTO Users (activation_token, login, email, password, name) VALUES (?, ?, ?, ?, ?)' (activation_token, request.form['login'], request.form['email'], hashed_password, request.form['name']))
should be:
cursor.execute('INSERT INTO Users (activation_token, login, email, password, name) VALUES (?, ?, ?, ?, ?)', (activation_token, request.form['login'], request.form['email'], hashed_password, request.form['name']))
I have also learned from this answer, that in order for INSERT
to actually happen, connection.commit()
should be called after cursor.execute('INSERT ...')
(single or multiple).
Thus, the final (revised) code, should look something like this:
db_connection = get_db()
cursor = db_connection.cursor()
hashed_password = werkzeug.security.generate_password_hash(request.form['password'], method='pbkdf2:sha512', salt_length=25)
activation_token = werkzeug.security.generate_password_hash(request.form['login'], method='pbkdf2:sha512', salt_length=25)
cursor.execute('INSERT INTO Users (activation_token, login, email, password, name) VALUES (?, ?, ?, ?, ?)', (activation_token, request.form['login'], request.form['email'], hashed_password, request.form['name']))
db_connection.commit()
cursor.close()
close_connection(None)
Upvotes: 1