user2242044
user2242044

Reputation: 9233

MySQLdb returning old data when used with Flask Post

I have a Flask website with a MySQL backend. I have a table called, users. It has two columns: username and name and one record:

name  username
Jim   testuser123

When a user clicks the button on the website, it updates the record to set the name to Bob then print all records where name = 'Bob'. Yet, it returns no results. If I refresh the connection before re-querying, then it does return one result as it should. Does the mdb.connect object cache data? How could it not be returning the correct results?

init.py:

import pandas as pd
import MySQLdb as mdb
from flask import Flask, render_template, request

def sql_con():
    return mdb.connect(host='myhost', port=3306, user='root', passwd='root', db='db', use_unicode=True, charset="utf8")

app = Flask(__name__)


def update_record():
    con = sql_con()
    cur = con.cursor()

    sql_string= "Update users set name = 'Bob' where username = 'testuser123'"

    cur.execute(sql_string)
    con.commit()


@app.route('/', methods=['GET', 'POST'])
def myroute():
    con = sql_con()

    if request.method == 'POST':
        update_record()
        print pd.read_sql("select * from users where name = 'Bob'", con=con)

    return render_template('1.html')

app.run( debug=True, port=5050)

1.html

<html>

<body>

    <form method="POST">
          <button id="mybutton" name='btn' value="mybutton">Submit Data</button>
    </form>
</body>

For this code to print one result, I must add con=sql_con() right after I call the update(), but before the print statement. Why is that?

Upvotes: 0

Views: 1048

Answers (1)

glenfant
glenfant

Reputation: 1318

In general it is a good practice to use an ORM binding (i.e. Falsk-SQLAlchemy) with web frameworks (manages connection pools, automates commit/rollback, ...) even if an ORM seems overkill for a simple application.

Otherwise, avoid using multiple connections to the same database in the same request if you prefer manage this at low level (database connections).

Try this instead:

import pandas as pd
import MySQLdb as mdb
from flask import Flask, render_template, request

def sql_con():
    return mdb.connect(host='myhost', port=3306, user='root', passwd='root', db='db', use_unicode=True, charset="utf8")

app = Flask(__name__)


def update_record(con):
    cur = con.cursor()

    sql_string= "Update users set name = 'Bob' where username = 'testuser123'"

    cur.execute(sql_string)
    con.commit()


@app.route('/', methods=['GET', 'POST'])
def myroute():
    con = sql_con()

    if request.method == 'POST':
        update_record(con)
        print pd.read_sql("select * from users where name = 'Bob'", con=con)

    return render_template('1.html')

app.run( debug=True, port=5050)

If you want to scale a real app based on such solution, you should consider pulling an opened connection from a global connections pool. Creating a new db connection (at each HTTP request) may be time expensive.

Upvotes: 1

Related Questions