import like this will not work.
Here some step for this.
I use sqlite so so can test without log in to eg postgresql.
Now for command line in same folder as app.py.
This is one one time operation here we create the database.
Test that it work.
Here some step for this.
# app.py from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' # //// 4 on Linux app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) class Person(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(), nullable=False) @app.route('/') def index(): return 'Hello World!' if __name__ == '__main__': app.run(debug=True)Se what i have changed like no
db.create_all()
in code.I use sqlite so so can test without log in to eg postgresql.
Now for command line in same folder as app.py.
This is one one time operation here we create the database.
>>> from app import db >>> from app import Person >>> db.create_all() >>> exit()Now have create database
teste.db
.Test that it work.
>>> from app import db >>> from app import Person >>> guest = Person(name='Kent') >>> guest1 = Person(name='Tom') >>> db.session.add(guest) >>> db.session.add(guest1) >>> db.session.commit() >>> exit()Look at test.db with sqlite command line tool.
λ sqlite3 test.db SQLite version 3.28.0 2019-04-16 19:49:53 Enter ".help" for usage hints. sqlite> .mode column sqlite> .headers on sqlite> select * from Person; id name ---------- ---------- 1 Kent 2 Tom