skip to Main Content

I am working on a application. I am using HTML, CSS, Jquery, Flask, MySQL-Python-Connector, Flask-Session. The data will be pushed into the database by some other application and this interface will only be used to view the records in the database. I need to restart the flask application every time a new record is pushed into the database else the old records keep showing up even after infinite page refreshes.

I thought it may be I was using the debug mode while running the application but that does not resolved the issue:
app.run(port=5000)

2

Answers


  1. Chosen as BEST ANSWER

    I resolved this issue by adding db.commit() line after fetching the results.

    The following was my previous code:

    cursor = db.cursor()
    cursor.execute(sql, tuple(params))
    results = cursor.fetchall()
    

    The following is my new code:

    cursor = db.cursor()
    cursor.execute(sql, tuple(params))
    results = cursor.fetchall()
    db.commit()
    

  2. You should avoid using pure MySQL connector for handling requests. For Flask it is recommended to use Flask-SQLAlchemy due to possibilities of handling app context.

    Try installing needed libaries by:

    pip install Flask-SQLAlchemy

    pip install mysqlclient

    Tutorial about Flask-SQLAlchemy: Link to YouTube.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search