I'm trying to make an SQLite database connection through Flask-sqlalchemy. I opened a command prompt where dbconnect.py is saved containing from dbconnect import app, db,Connect
and db.create_all()
. Then I opened sqlite3.exe (same location as the Python file) and did db.connect
and .tables
.
I was expecting Connect table to be listed but nothing came up. I can see the data in Visual Studio Code though. Where am I going wrong?
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.connect'
db = SQLAlchemy(app)
class Connect(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(20),nullable = False)
with app.app_context():
db.create_all()
entry = Connect(name = "Apple")
db.session.add(entry)
db.session.commit()
dbdata = Connect.query.all()
print(dbdata[0])
if __name__ == '__main__':
app.run(debug = True)
Do you use the .mode column
command? Command-Line doc
On windows double click sqlite3.exe
opens the command line shell
than in the shell use your dot commands, like:
sqlite> .open c:\your_path\databasename.db # connect your existing database
sqlite> .tables # will show you the existing tables list.
sqlite> .mode column # set the table format for output
sqlite> select * from your_tablename;