jeudi 27 décembre 2018

How to delete the database every time when the terminal is closed?

The app stores the messages entered by the client. But, every time I run the app, it also loads the previous data. I want to clear the database every time I close the app.

This is a basic chat app where I display the messages entered by the user. But it keeps storing the messages in the same data base. I've tried to turning on the auto-flush but that didn't work.

app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///messages.db

db = SQLAlchemy(app, session_options = {"autoflush":True})

This is my db model:

@app.route('/message', methods=['POST'])
def message():

try:

    username = request.form.get('username')
    message = request.form.get('message')

    new_message = Message(username=username, message=message)
    db.session.add(new_message)
    db.session.commit()
    #db.session.expire()


    pusher_client.trigger('chat-channel', 'new-message', {'username' : username, 'message': message})

    return jsonify({'result' : 'success'})

except:

    return jsonify({'result' : 'failure'})

This is the part of the template which I'm returning for printing the contents of the database:

  <div id="content" class="container" style="overflow-y:auto; margin-bottom: 100px;">

  

I want to earse the content of the database every time the fask app is closed.




Aucun commentaire:

Enregistrer un commentaire