To save Datatable to mysql database in Dash Multipage app. But raise db is not defined error

You need to do this:

with app.app_context():
db.create_all()

Instead of just db.create_all()

@jinnyzor

Did I apply right way?

Try it from a file. It might not work in the terminal any more

Like that?

But when I run this code I got this error:

KeyError: <weakref at 0x00000161A07F1D50; to ‘Flask’ at 0x000001619CAB3A30>

and my other pages turned to 404

@jinnyzor
Finally I got this error

KeyError: <weakref at 0x00000161A07F1D50; to ‘Flask’ at 0x000001619CAB3A30>

Do you have any suggestion or documentation for solve this connection of database.

@Sanan009,

Try this in a new scratch file by itself. And take it out of your pages.

from app import app, db
with app.server.app_context()
    db.create_all()

@jinnyzor Sorry,
Mr. Bryan this is the same result. Nothing changed

This should be:

db = SQLAlchemy()

Error

sqlalchemy.exc.UnboundExecutionError: 'SQLALCHEMY_DATABASE_URI' config is not set. Bind key 'None' is not in 'SQLALCHEMY_BINDS' config.

@jinnyzor

Mr.Bryan can you explain this error meaning?

Take from python site:

A weak reference to an object is not enough to keep the object alive: when the only remaining references to a referent are weak references, garbage collection is free to destroy the referent and reuse its memory for something else. However, until the object is actually destroyed the weak reference may return the object even if there are no strong references to it.

Let’s change this line:

app.server.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:test12345@localhost/searchdb"

to this:

app.server.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///project.db"

Then try the script to create the db again.