[Python] SQL, Flask -- database table doesn't show up

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I recently made a login app, and in the run section I made several checks to make sure that the table "user" is indeed created as the output prints

    Database creation attempted
    Existing tables: ['user']
    Database URI: sqlite:///database.db


    however, when I entered

    sqlite3 database.db .tables


    nothing showed up.

    This is my original code:

    #Initializing
    app = Flask(__name__)
    bcrypt = Bcrypt(app)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
    db = SQLAlchemy(app)
    app.config['SECRET_KEY'] = 'secretkey'

    #Creating the user class
    class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), nullable=False, unique=True)
    password = db.Column(db.String(50), nullable=False)

    #Running the app
    if __name__ == "__main__":
    with app.app_context():
    db.create_all()
    print("Database creation attempted")

    # Check if table exists
    inspector = inspect(db.engine)
    tables = inspector.get_table_names()
    print("Existing tables:", tables)
    print("Database URI:", app.config['SQLALCHEMY_DATABASE_URI'])
    app.run(debug=True)


    I tried adding multiple checks (for example printing "table created" or even printing the path of the table) but it all didn't work. I would be very happy if someone can help me out.

    Continue reading...

Compartilhe esta Página