How to Use Databases With SQLAlchemy - Flask Fridays #8

preview_player
Показать описание
In this video I'll show you how to use databases with Flask using SQLAlchemy!

Using databases with flask is a little harder than with other web frameworks, but it doesn't have to be too bad if you use SQLAlchemy. I'll show you how in this video by setting up a SQLite database and creating a table, and then adding a form to the site that allows us to add data to the database.

After we learn how to do this with SQLite, we'll learn about MySQL and Postgres.

Timecodes
0:00​​ - Introduction
1:13 - Pip Install Flask-SQLAlchemy
1:56 - Import SQLalchemy To Our Flask App
2:55 - Set SQLAlchemy Database URI For SQLITE
4:20 - Initialize The Database
4:50 - Create Our Database Model
9:39 - Import Model From The Terminal
10:38 - Create UserForm
11:25 - Create Route And Add_User Function
14:22 - Set Form to UserForm in Add_User Function
15:00 - Validate Form On Submit
16:54 - Save Data To Database On Submit
18:50 - Query Database And Output Users To Webpage
21:37 - Style Form With Bootstrap
22:37 - Add Table For User Output
24:46 - Conclusion
Рекомендации по теме
Комментарии
Автор

▶️ Watch Entire Flask Friday Playlist ✅ Subscribe To My YouTube Channel:
▶️ See More At: ✅ Join My Facebook Group:
▶️ Get The Code

Codemycom
Автор

I usually play educational videos at x1.5 speed. I want to thank you for being so thoughtful as to provide it as the default option! Also, great content!

todorvladimirov
Автор

Just want to say that after so long these videos are still helping people learn. Thanks for keeping this series available.

witty_name
Автор

I dont understand the people using 1.5x speed for educational videos. Take it easy man, nobody is trying to catch you. Besides, you dont want to rush these things. It's true, you will finish the lesson 1.5 times faster but you will forget it all 2 times faster if you dont sit and practice. No rushing, keep focused.

Great video tough, keep it up!

ugrasarks
Автор

You really dont know how golden this is <3 love from Saudi Arabia

SAMIGAMER
Автор

Hi, I am unable to create db.. using db.create_all().... I am getting following error : " This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context with app.app_context()." ... I have tried to use with function
def create_app():
app = Flask(__name__)
with app.app_context():
...,
but it doesnt resolve the error.. can you please help

macmohit
Автор

Thank You!
Please make a Complete Machine Learning Series

parthivlakhani
Автор

Hello, i want to ask about returning the database as string (at 8:58). Could you explain the syntax? Also is it must be self.name?

wongjowo
Автор

Awesome! flask-sqlalchemy is great! Thanks for the tutorial!

karlduckett
Автор

while running the db.create_all() command i got the "RuntimeError: Working outside of application context". please help me out. I have been following along with the Flask Friday since day 1.

SharifaNazari-gh
Автор

Very helpful! It would've been really helpful to have a larger code window, the panning along with the typing was really not ideal. Maybe there is a reason why you have a zoomed in window, but most tutorials look fine with a full sized window. Thank you for the helpful content! thumbs up.

diddlybop
Автор

sir, not gonna lie, you and Heisenberg(Walter White) look uncannily similar.

scard
Автор

I am having a hard time with that command in the terminal winpty python. I have a mac. I know this is meant for windows. Is there another way to create the database?

angela_rodriguez
Автор

Hello john. Thank you for your video on flask and other YouTube videos you have been releasing. However, i have a problem with the initialization and creation of the databse as instructed in this video at time 10:20. When i tried to create my database it kept bringing up the error below;
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.
is there anything am not doing right? i"l appreciate your prompt response as usual.
Thank you

wingielee
Автор

When added an email second time, it's not getting added but getting the same message 'User Added Successfully'. What can I do?

pandurangasharmaramaka
Автор

UPDATE
I was getting errors when trying to create the database. I put this in my code:
with app.app_context():
db.create_all()
I added this (for a single run) underneath the class that defined the db.Columns().
I also may have ran db.init_app(app) in between that and the class (or at some point in cmd - importing both db and app), I don't remember where or if that helped or not.
Either way, I got it working now (5/5/23)

sevii
Автор

I NEED SOME SERIOUS HELP. When I entered "from hello import db" I get nothing, no response at all. no errors, but no confirmation as well and when i tried the create_all(), I got a bunch of errors. I can't proceed. I looked it up myself but I can't find anything useful. I even tried to skip using sqlite and head over the mysql one, but the same problem persist. Please help.

georlybibas
Автор

Thank you so much for the tutorial, it helped me a lot at uni! I'm getting an error:

File "/Users/karo/Library/Python/3.8/lib/python/site-packages/werkzeug/local.py", line 513, in _get_current_object
raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.


Do you have any suggestions, please? I've tried about 10 stackoverflow answers but nothing works for me. From my research I believe this is an issue that happens because of a recent update to SQLAlchemy?

karolinalencina
Автор

Should db.create_all() be executed through the python shell always (to create tables)? Or can we do that in our source code? Because if we deploy the webapp somewhere will we have to explicitly run db.create_all() in the python shell before running the webserver in deployment?

alanalmeida
Автор

Hi! This is really great and I am following it for my first flask app project, but I've made my databases + tables in the MySQL workbench and I'm having a lot of trouble connecting the app to my tables (i.e. sending the outputs of my form to the tables initiated in workbench). Would you please add an explanation on how to connect to an existing table (instead of creating one directly in the script)? Thanks a lot!

kristynaherman
visit shbcf.ru