I am currently writing a program in python using flask and flask_sqlalchemy. I have done the same steps as in the documentation. But when the database gets created automatically, it has an unknown file type, altough it should be a sqlite database. I am using Pycharm btw.
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
# create the extension
db = SQLAlchemy()
# create the app
app = Flask(__name__)
# configure the SQLite database, relative to the app instance folder
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///filemanager.db"
# initialize the app with the extension
db.init_app(app)
class File(db.Model):
id = db.Column(db.Integer, primary_key=True)
path = db.Column(db.String, unique=True, nullable=False)
type = db.Column(db.String, unique=False, nullable=False)
@app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
if __name__ == '__main__':
create_database = True
if create_database:
with app.app_context():
db.create_all()
app.run(debug=True, port=5008)
I tried to change the file type manually to sqlite, but it still doesn't contain tables and columns. If I create the columns manually in the console everything works, but I have and want to do it programmatically. I also tried another stackoverflow answer, where he split the code into two files, didn't work eiter. Thanks in advance!
CodePudding user response:
The problem was the name of the Database and the name of the table. I had "filemanager.db" and "files", these names don't work (don't ask me why), but I tried with "users.db" and "users" and it works