0

I'm a Flask newbie trying to create a simple app. I'm currently stuck at user registration where I'm trying to save data in database but it's not happening. However, the logging I'm doing indicates that the operation was a success. Could someone tell me what I'm doing wrong?

Here are portions of code that'll help you understand what I'm trying to do:

from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from flask.ext.mysqldb import MySQL

# Configuration
MYSQL_HOST     = 'localhost'
MYSQL_USER     = 'root'
MYSQL_PASSWORD = 'root'
MYSQL_DB       = 'up2date'
DEBUG          = True
SECRET_KEY     = 

'\xc6)\x0f\\\xc5\x86*\xd7[\x92\x89[\x95\xcfD\xfd\xc1\x18\x8e\xf1P\xf7_\r'

# Create the flask app
app = Flask(__name__)
app.config.from_object(__name__)
# Create instance for working with MySQL
mysql = MySQL(app)

# Function to connect to DB
def connect_db():
    return mysql.connection.cursor()

# define functions that will make DB available automatically on each request
@app.before_request
def before_request():
    g.db = connect_db()

@app.teardown_request
def teardown_request(exception):
    g.db.close()

And finally, the code that performs user registration:

@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        email = request.form['email']
        password = request.form['password']
        result = g.db.execute('INSERT INTO users (email, password) VALUES (%s, %s)', [email, password])
        print(email, password)
        print(result, " rows affected")
        flash('Registration successful! You may log in now.')
        return redirect(url_for('show_home'))

The two print statements confirm that the email address and password were captured correctly, and the result variable contains 1, indicating 1 row affected. But still there's no row in the DB. I earlier thought this had something to do with committing, but g.db.commit() throws error: AttributeError: 'Cursor' object has no attribute 'commit'

2
  • Can you provide the error of the commit. Commented Dec 15, 2015 at 17:10
  • @ipinak Added to the end of my post! Commented Dec 15, 2015 at 17:13

1 Answer 1

2

I assume you use MySQL-python.

connect_db() returns the cursor and not the connection. The cursor does not have a commit() function, as the exception says, however the connection has the commit function you need. I think you need to do this:

def connect_db():
   return mysql.connection

For more info you can take a look at the code.

Sign up to request clarification or add additional context in comments.

3 Comments

My bad for not mentioning the library. I'm actually using flask-mysqldb. I've added the import statements to the top of my code.
Flask-MySQL uses MySQL-python, so it does not matter. Did my suggestion work?
Well, well! Thanks a lot! It worked finally. :D I only had to make changes to my code to ensure execute() was called on g.db.cursor() and your suggestion worked. Thanks a lot for helping me. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.