#๐ Connecting SQlite database to flask
54 messages ยท Page 1 of 1 (latest)
@viral wharf
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
You should be much more descriptive about where you are at and what immediate problem you are facing, or people won't be very likely to respond to your help thread.
Since I helped you before, I know there's more to it than what you've written in your initial message, so I know there's a lot more you could talk about.
I donโt really know what else to say, i need to connect the schema.sql file to my main flask app so it can access the database
I donโt fully know what iโm doing so itโs hard to describe
Your schema.sql file does not connect directly to the database. Rather, that file contains SQL code describing the schema of your tables and their columns (I'm assuming based on its name, anyway).
You interact with a database using SQL statements, such as the ones you might find in that file.
Flask doesn't have a special method for connecting to the database: you would do that yourself using, in this case, the sqlite3 module built into Python.
There's a tutorial for that module on the Python docs site here: https://docs.python.org/3/library/sqlite3.html#sqlite3-tutorial
Perhaps another resource tailored to SQL basics might help, as well.
One thing you can do is include all the relevant code that you already have.
Okay give me a minute to get it all
You may come back again later, so for future reference, you should just put that in your initial message(s)
schema.sql:
CREATE IF NOT EXISTS league;
CREATE IF NOT EXISTS games;
CREATE IF NOT EXISTS teams;
CREATE IF NOT EXISTS players;
CREATE TABLE league (
league_id INTEGER PRIMARY KEY AUTOINCREMENT,
team_id REFERENCES teams (team_id),
wins INTEGER DEFAULT 0,
draws INTEGER DEFAULT 0,
losses INTEGER DEFAULT 0,
games_played INTEGER DEFAULT 0,
points INTEGER DEFAULT 0
);
CREATE TABLE games (
game_id INTEGER PRIMARY KEY AUTOINCREMENT,
date_time DATE NOT NULL,
hometeam_id REFERENCES teams (team_id),
awayteam_id REFERENCES teams (team_id),
hometeam_singles_score INTEGER NOT NULL,
awayteam_singles_score INTEGER NOT NULL,
hometeam_doubles_score INTEGER NOT NULL,
awayteam_doubles_score INTEGER NOT NULL
);
CREATE TABLE teams (
team_id INTEGER PRIMARY KEY AUTOINCREMENT,
teamname VARCHAR(50)
);
CREATE TABLE players (
player_id INTEGER PRIMARY KEY AUTOINCREMENT,
team_id REFERENCES teams (team_id),
playername TEXT NOT NULL
);
__init__.py:
import os
from flask import Flask
def create_app(test_config=None):
# Create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='13a18b15677184599b28e708546b363d500ea19824b098fcaf773e3580cb45ef',
DATABASE=os.path.join(app.instance_path, 'darts.sqlite'),
)
if test_config is None:
# Load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# Load the test config if passed in
app.config.from_mapping(test_config)
# Ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# A simple home page
@app.route('/home')
def hello():
return 'Section B league home page!'
return app
those are my two files right now
in the darts-league folder
okay i need to register the database with the application (flask)
and then initalise it
i just need someone to help me do that
What about your db file from before?
You can refer back to the tutorial you went through before, and the blog app you made
Compare the code and see what differs
You don't register the database with flask, flask doesn't know about databases, it's just for handling web requests
You didn't register the database with flask in the blog either
Thats what the subtitle says though?
You registered a CLI command that called a function you wrote which initiated the database, and you registered a function you wrote to be called when the flask app shuts down, which closes the database connection
So in neither case does Flask itself know about the database
It just knows about some functions that you want to call when certain things happen (like when your run a console command, or when the Flask app closes)
And all the database stuff happens inside those functions
You can also look at your blog code to see how to use the database connection in response to web requests, like when you add a new blog post for example
Yeah, im getting an error when running the command
If you want help with an error in general, you should always just copy and paste the error into the chat
Because that's always gonna be necessary anyway to help you
damn its too long
File "C:\Users\georg\OneDrive\Documents\GitHub\Darts-League-Project\darts-league\__init__.py", line 32, in create_app
db.init_app(app)
^^^^^^^^^^^
AttributeError: module 'darts-league.db' has no attribute 'init_app'. Did you mean: 'init_db'?
PS C:\Users\georg\OneDrive\Documents\GitHub\Darts-League-Project>
thats the last bit
Well, what does the error message say?
It says about darts-league.db but that isnt created anywhere
darts-league is the name of the package, since db.py is in a directory called darts-league, which contains an __init__.py file
okay i see
In Python, a module is just a single file that contains Python code โ like functions, classes, or variables โ that you can use in other programs. A package is a collection of modules grouped together in a folder with a special file called
__init__.py, which helps Python recognize it as a package. Think of modules as individual tools, and packages as organized toolboxes full of related tools. They make it easier to keep code organized and reuse it across different projects.
Nice
Yes, it's created automatically to keep track of AUTOINCREMENT fields
Ahh okay I see
So, i will move onto the next step of the tutorial and see how i link it into my project
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.