new programmmmmer new programmmmmer - 1 month ago 5
Python Question

Where is app.sqlite located?

Hi guys I've been following this old tutorial. Can someone tell me where app.sqlite will be located. Also is app.sqlite a database that I have made I'm not sure because I wanna wanna find it and checked if the table is made. Also is there a wayto change the location to where i want it :D

This is the config file.

import os
basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.sqlite')
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')


And this is the file where I try to make RESTful API, I haven't put anything in yet though since I'm lost.

from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_pyfile('Config.py')
db = SQLAlchemy(app)

class User(db.Model):
User_ID = db.Column(db.Integer, primary_key = True)
firstName = db.Column(db.String(20))
lastName = db.Column(db.String(20))

def __init__(self,firstName, lastName):
self.firstName = firstName
self.lastName = lastName
db.createAll()

if __name__ == '__main__':
app.run()

Answer

You can place app.sqlite in the same directory as your main python file. To create all of the tables, you can add the following method to your code:

@app.before_first_request
def startup():
    db.create_all()

This will run when you start your server and will create all of the tables. If you restart the server, the method will still run, but the tables won't be remade since they already exist.

Comments