Тёмный
No video :(

Using Databases With Flask - Python and Flask #8 

Codemy.com
Подписаться 232 тыс.
Просмотров 76 тыс.
50% 1

In this video I'll show you how to start using Databases with Flask and Python.
We'll be creating a database to keep track of our Friends list. We'll use sqlite3 and sqlalchemy for the database.
Using a database in flask is fairly easy, you just add sqlalchemy to your app, then create a model that defines your database table and columns. Then adding and retrieving things from the database is trivial.

Опубликовано:

 

15 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 139   
@Codemycom
@Codemycom 4 года назад
▶️ Watch Entire Flask Playlist ✅ Subscribe To My RU-vid Channel: bit.ly/2TU96lm bit.ly/2IGzvOR ▶️ See More At: ✅ Join My Facebook Group: Codemy.com bit.ly/2GFmOBz ▶️ Learn to Code at Codemy.com ✅ Buy a Codemy T-Shirt! Take $30 off with coupon code: youtube1 bit.ly/2VC9WUN
@JoshKonoff1
@JoshKonoff1 3 года назад
Do you have a Patreon page?
@Codemycom
@Codemycom 3 года назад
@@JoshKonoff1 No, but you can sign up for a membership at my website Codemy.com
@takodamundy4506
@takodamundy4506 2 года назад
Just thought i'd thank you for carrying me through one of my CS assignments, cheers, I'd be in the bin without these videos
@Codemycom
@Codemycom 2 года назад
Happy to help!
@df6148
@df6148 Год назад
This force is very strong with this one. Indeed a Jedi of his craft. Indeed is he.
@Codemycom
@Codemycom Год назад
lol thanks!
@faithnjoki5573
@faithnjoki5573 2 года назад
I had problems with understanding flask, You made it so easy for me. I now get it .. From the botom of my heart, Thank you... Happy new year
@Codemycom
@Codemycom 2 года назад
You're welcome! Happy New Year!
@Flamiooo
@Flamiooo 4 года назад
I had been waiting for this video for so long, finally
@Codemycom
@Codemycom 4 года назад
Glad you liked it!
@kwabenankyi
@kwabenankyi 2 года назад
very simple and easy to follow tutorial, thank you very much
@Codemycom
@Codemycom 2 года назад
very welcome!
@jean-paullavalley9848
@jean-paullavalley9848 2 года назад
I get the following error a 11min in when you are using the gitbash terminal to create the "db ImportError: cannot import name 'db' from 'app' (C:\my_flask\app.py)"
@jean-paullavalley9848
@jean-paullavalley9848 2 года назад
from flask import Flask, render_template, request #import smtplib from flask_sqlalchemy import SQLAlchemy from datetime import datetime app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///friends.db' #Initialize the database db = SQLAlchemy(app) #Create db model class Friends(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200), nullable=False) date_created = db.Column(db.DateTime, default=datetime.utcnow) #Create a function to return a string when we add something def __repr__(self): return "" % self.id
@danielhansen7519
@danielhansen7519 3 года назад
Say that your friends.html page has multiple types of input and you had another .html page that has only one input variable that also posts to your friends.html. If you were to have two different .html pages going to your friends.html with the post method, how could you distinguish between them? Say, friends.html would update the database and itself but the other .html would bring only a friends name and you were to have a dropdown menu which default value needs to be that posted friends name. How would you do that?
@TechWithSabri
@TechWithSabri 4 года назад
Thank you for sharing your knowledge Sir we love you
@rezaghasemzadeh9440
@rezaghasemzadeh9440 2 месяца назад
very useful, thanks
@Codemycom
@Codemycom 2 месяца назад
Glad it was helpful!
@tochukwunwokolo2048
@tochukwunwokolo2048 4 года назад
Hi, thanks for your easy-to-follow videos, could you do a video for searching through a database with Flask?
@Codemycom
@Codemycom 4 года назад
Glad you're enjoying them! I might do something like that in the future...
@benjaminodriscoll6605
@benjaminodriscoll6605 3 года назад
Explained really clearly, thanks alot!
@Codemycom
@Codemycom 3 года назад
Glad it helped!
@ciyaturgut
@ciyaturgut 3 года назад
Man i love your flask videos
@Codemycom
@Codemycom 3 года назад
Thanks!
@kshitijmathur1358
@kshitijmathur1358 3 года назад
It would be cool to inform the last commit on starting the tutorial - so we pick up exactly where you are starting from.
@MarkTw20
@MarkTw20 3 года назад
Hi, I wanna know how to make a gallery (flask-html) using images taked from a sql database (sqlite3) Thanks
@antoinekikkers9022
@antoinekikkers9022 2 года назад
If I made a User model and created it via de Python Interpreter. How can I add a new rows? For example, if you wanted to add a row 'Gender' to your existing database after you did the command db.create_all()?
@Onecool0
@Onecool0 2 года назад
Hey, where can I get code files?
@marlegagaming1274
@marlegagaming1274 3 года назад
Quite Informative, Any reason why you're not using PyCharm or VSCode, would make life easier with imports etc..,
@Codemycom
@Codemycom 3 года назад
Because no, imports are harder with those things lol what could be easier than the way I do it now? I don't have to fudge with VSCode or Pycharm settings or installation hassles.
@pikachubooo4308
@pikachubooo4308 3 года назад
It’s showing “ there was an error adding your friend...” How to resolve it?
@pikachubooo4308
@pikachubooo4308 3 года назад
Got resolved!
@supersentaimexicano1967
@supersentaimexicano1967 Год назад
I'm having an error when tyring to import db >>> from app import db C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\flask_sqlalchemy\__init__.py:851: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:". Can someone help me pls?
@MohamedAshraf-zs6nv
@MohamedAshraf-zs6nv 3 года назад
what if I have multiple apps in the Blueprint structure, how can I create a database with its model for each app separately ?
@akramelomrani8728
@akramelomrani8728 3 года назад
Finnaly some databases tnx man
@Codemycom
@Codemycom 3 года назад
Sure thing
@parthdutta8758
@parthdutta8758 2 года назад
When someone put the details in our subscribe web page, how to store that information in database?
@victorcirillo1016
@victorcirillo1016 Год назад
Can you help me with this error? ModuleNotFoundError: No module named 'app'
@selmamilena2503
@selmamilena2503 Год назад
i like his energy
@Codemycom
@Codemycom Год назад
Thanks!
@ophirn.m7817
@ophirn.m7817 2 года назад
Hi I love your videos! But I am having a problem... the db file is not creating after the db.creat_all() can you please help me?
@nezhatnour2462
@nezhatnour2462 2 года назад
deactivate virtual env and pip install sql! activate it and it will work
@supersentaimexicano1967
@supersentaimexicano1967 Год назад
@@nezhatnour2462 >>> db.create_all() Traceback (most recent call last): File "", line 1, in NameError: name 'db' is not defined This error still pops up after
@narendrareddy8420
@narendrareddy8420 3 года назад
how to connect to existing database(SQL) with SQLALchemy?
@fouadlhort5439
@fouadlhort5439 4 года назад
sir when i write "from app import db" i get the error "ImportError: cannot import name 'SQLALchemy' from 'flask_sqlalchemy'" pls help me
@fouadlhort5439
@fouadlhort5439 4 года назад
i google it and i didn't find the solution
@Codemycom
@Codemycom 4 года назад
Did you do it the same way I did it in the video? Exactly?
@devan8166
@devan8166 3 года назад
Sorry if this is late, but it looks like you imported “SQLALchemy” instead of “SQLAlchemy”
@eshaangupta4101
@eshaangupta4101 3 года назад
Love your videos.
@Codemycom
@Codemycom 3 года назад
Glad you like them!
@dktitanx2773
@dktitanx2773 2 года назад
i'm on mac osx and having trouble using "winpty python". is there a mac version of this command or do i have to take extra steps to install the necessary libraries for this command?
@Codemycom
@Codemycom 2 года назад
leave off the winpty, that's just a windows thing
@bolatbekminbayev9217
@bolatbekminbayev9217 3 года назад
I am getting an error(ModuleNotFoundError: No module named 'flask_sqlalchemy') but I have already installed. Can u help me?
@vasiliystepanov7423
@vasiliystepanov7423 3 года назад
Try to use "pip install Flask-SQLAlchemy" in GIT BASH terminal.
@justash123
@justash123 2 года назад
Have u done from flask_sqlalchemy import SQLAlchemy
@matthewbraymer1136
@matthewbraymer1136 Год назад
can this work with programs such a SQLight Studio?
@Codemycom
@Codemycom Год назад
yes, that's the default db
@chotagunnu7489
@chotagunnu7489 2 года назад
can you point to where is this database series listed since I could not find sqlite database related coverage in the Flask course on your website.
@Codemycom
@Codemycom 2 года назад
ru-vid.com/group/PLCC34OHNcOtolz2Vd9ZSeSXWc8Bq23yEz
@alfredsyriac
@alfredsyriac Год назад
how to access db object in sub modules
@ayushagarwal4958
@ayushagarwal4958 3 года назад
I am not able to display the information after submitting the form. Can you tell me where I could be wrong? I followed the video step by step but instead I just used more variables than just a name.
@Codemycom
@Codemycom 3 года назад
No I couldn't guess. Try redoing it exactly as the video shows and see if you can get that to work before trying something differently.
@cricketer1987
@cricketer1987 Год назад
I'm currently at 12:20 and I've followed all of these steps and get errors so I've decided to follow some troubleshooting steps on StackOverflow to the point where.I do db.create_all() I get no errors. However, for some reason the database doesn't appear on my file explorer nor my IDE project explorer (PyCharm). Has the database been created? Thanks. UPDATE - can confirm the database was created as searched for 'friends.db' in finder on Mac.
@Codemycom
@Codemycom Год назад
Two things: 1. If you follow along with the videos, you won't need to troubleshoot. You're having errors because you aren't following the directions. Just to be blunt. Just rewatch the videos and try again. 2. Sorry, I don't use pycharm or recommend it (for this reason). So I can't guess why it is doing what it's doing. Just use the tools I use. They're free.
@cricketer1987
@cricketer1987 Год назад
@@Codemycom thanks for the reply. I can reassure you I have followed all directions and am just using a different IDE. Not saying it’s an issue with your tutorial but rather may be a common error when people watch this video if they are using an IDE such as PyCharm so just pointing it out in the comments. The main error was the Python terminal couldn’t find Flask for some reason.
@Codemycom
@Codemycom Год назад
@@cricketer1987 Yeah it could be Pycharm...but also...I get hundreds of comments and emails from people SWEARING that they followed the directions...then later saying "oh, whoops, I left out a comma". If it didn't work, it's likely because you missed something. It's no big deal, we all do it.
@manasavallampalli
@manasavallampalli 3 года назад
So helpful!
@Codemycom
@Codemycom 3 года назад
Thanks! Glad you found it so..
@tatividumaamey5828
@tatividumaamey5828 Год назад
Sir i got unresolved reference for SQLAlchemy
@mahadevkalshetty4943
@mahadevkalshetty4943 3 года назад
sir kindly help me i'm getting error: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file (Background on this error at: sqlalche.me/e/13/e3q8)
@AKhan-3
@AKhan-3 3 года назад
Great!!!
@stormschaser
@stormschaser 4 года назад
I have trouble in loading the data into db, what I get from db is , etc.. rather than the info that I entered into the form...
@Codemycom
@Codemycom 4 года назад
Weird. What did you do differently than the videos?
@stormschaser
@stormschaser 4 года назад
@@Codemycom I figured it out. In my case, I created multiple columns in the DB, hence to fetch data from it, i need to specify the column name (record.column1, record.column2), otherwise it will just give me etc which representing the whole row of record
@MagicByIzzy
@MagicByIzzy 4 года назад
When I try to set default=datetime.utcnow I get an error saying the equals sign does not belong there.
@Codemycom
@Codemycom 4 года назад
did you google the error?
@MagicByIzzy
@MagicByIzzy 4 года назад
@@Codemycom no also I would like to add a subject line to the emails and I couldn't find the solution
@raselahmed2221
@raselahmed2221 3 года назад
I get error.. [ no module found MYSQLdb] How can I solve
@Codemycom
@Codemycom 3 года назад
follow the instructions in the video exactly
@davidbeiler6364
@davidbeiler6364 2 года назад
do you have a video on how to remove a friend from the list?
@Codemycom
@Codemycom 2 года назад
Maybe not specifically, but many videos on the other playlist of deleting items from a database
@felixutomo6365
@felixutomo6365 3 года назад
can you help me with my css, i write this on my code when i change background to blue (from the previous tutorial) it changed, but when i want to change it to white in this tutorial, it wont change, and stay at dark blue, even when i deleted the css file it still doesnt change, my background still dark blue :'(
@Codemycom
@Codemycom 3 года назад
Shift+Reload the page, if that doesn't work restart your browser, if that doesn't work, clear your browser cache
@felixutomo6365
@felixutomo6365 3 года назад
@@Codemycom thankyou, it works now :)
@felixutomo6365
@felixutomo6365 3 года назад
@@Codemycom umm, i got problem again :'( pls help, i use flash messages, so when i want to insert data it will show a flash messages, i can fill my flash messages, but it doesn't show up 😭, i'm still learning so i don't have money to learn from many source :'(
@Codemycom
@Codemycom 3 года назад
@@felixutomo6365 Don't know man, there's got to be a typo in your code then.
@felixutomo6365
@felixutomo6365 3 года назад
@@Codemycom okk thankyouuu my fav youtuber, i already fix it, anyway i tried ur code, and when i run it, flask doesn't run, it just showed like this: flask run (virtual) without showing any errors or server
@rdubitsk
@rdubitsk 3 года назад
Why do you have to create a db using gitbash? Why can't it be written in the code?
@Codemycom
@Codemycom 3 года назад
That's how it's done
@rdubitsk
@rdubitsk 3 года назад
@@Codemycom I'm just think that if DB connection crashes if you just rereun the flask app.py (or whatever) it would create the database. I would have thought the gitbash command to create the database can be directly embedded in the flask file code. Obviously not.
@misterbristles
@misterbristles 3 года назад
What is the best way to add multiple user inputs as far as the db.session.add is concerned in the app.py file? i.e. db.session.add(this_db_column, that_db_column, this_other_db_column)
@misterbristles
@misterbristles 3 года назад
Here we go, got it. Hope this helps someone. pastebin.com/Yca8xUih
@teclote
@teclote Год назад
Please don't delete Bob.
@mbonuchinedu2420
@mbonuchinedu2420 3 года назад
Beautiful 💞💞💞
@Codemycom
@Codemycom 3 года назад
Thanks!
@hans.k3534
@hans.k3534 2 года назад
Just wonder, if anyone is able to run on google app engine?
@Codemycom
@Codemycom 2 года назад
why not?
@akramelomrani8728
@akramelomrani8728 3 года назад
Not showing my database c:\users\medion\desktop\wb env\virtual\lib\site-packages\flask_sqlalchemy\__init__.py:812: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:". warnings.warn( EDIT:never mind i forgot the A in DATABASE
@Codemycom
@Codemycom 3 года назад
ha, glad you got it sorted out
@andychang2739
@andychang2739 3 года назад
@@Codemycom Hi there. I have the same error message as Akram, but I am fairly certain that I have spelt everything correctly. Is there any other possible issues? Thanks!
@Codemycom
@Codemycom 3 года назад
@@andychang2739 Probably not
@tonymonta3
@tonymonta3 3 года назад
@@Codemycom i have the same issue, everything is correctly spelt
@neilgriffin9502
@neilgriffin9502 2 года назад
ModuleNotFoundError: No module named 'flask_sqlalchemy' ....
@Codemycom
@Codemycom 2 года назад
What did you do differently from the video?
@neilgriffin9502
@neilgriffin9502 2 года назад
@@Codemycom I'm sorry. I installed it correctly. Installing in pycharm worked.
@Codemycom
@Codemycom 2 года назад
@@neilgriffin9502 Apparently you didn't, or you wouldn't have an error
@ludvignordqvist1552
@ludvignordqvist1552 4 года назад
i get "500 internal server error" when i created the db and go the friends page.
@Codemycom
@Codemycom 4 года назад
You missed something then...try retracing your steps
@ludvignordqvist1552
@ludvignordqvist1552 4 года назад
@@Codemycom i retrace and found one wrong. But get error, no such table: friends return render_template("friends.html", friends=friends)
@programminginmymind2446
@programminginmymind2446 4 года назад
@@ludvignordqvist1552 PLEASE HELP me i have the same error :(
@programminginmymind2446
@programminginmymind2446 4 года назад
@@ludvignordqvist1552 and i dont know why
@MrSharkrat
@MrSharkrat 3 года назад
really usefull video thanks!! when i hit the "add name" button, nothing happend. Someont know why?
@Codemycom
@Codemycom 3 года назад
Glad you liked it. nothing happened because you have an error in your code. No one can guess what that error is...likely a typo
@MrSharkrat
@MrSharkrat 3 года назад
@@Codemycom true: i had written "type = "submit"" and not "type="submit"". The blanc between the "=" was the problem
@MagicByIzzy
@MagicByIzzy 4 года назад
It won't let me import the db When I try it runs the server and exits python
@Codemycom
@Codemycom 4 года назад
What exact error do you get?
@MagicByIzzy
@MagicByIzzy 4 года назад
@@Codemycom Can't find main method in my project directory
@MagicByIzzy
@MagicByIzzy 4 года назад
I had to a conditional saying if __name__ = "__main__" app.run
@MagicByIzzy
@MagicByIzzy 4 года назад
Now it workd
@Codemycom
@Codemycom 4 года назад
@@MagicByIzzy interesting
@m-jay356
@m-jay356 2 года назад
nice doggy
@Codemycom
@Codemycom 2 года назад
Thanks
@gamerstrim
@gamerstrim 4 года назад
Where is the Django?
@Codemycom
@Codemycom 4 года назад
This is a Flask video, not Django
@gamerstrim
@gamerstrim 4 года назад
@@Codemycom I understand. I wait Django video.
@sudeeptimsina4600
@sudeeptimsina4600 4 года назад
can you create tutorial on simple user management system in flask api something like this ------------------------- I have the following tables: Table name= group Attributes: id name Here simply the group is created. ------------------------------------------------------------------------------ Table name = group permission Attributes: id group_id Permission_id Here simply the permission for the group is assigned ----------------------------------------------------------------------------- Table name =permission Attributes: id name code name Here permission is created Table name =user Attributes: id username password ------------------------------------------------------------------------------- Here the user is created Table name =user_groups Attributes: Id User_id Group_id Here the group for the user is assigned and all the permission inside this group should also be assigned to that user -------------------------------------------------------------------------------------------------------------------------- Table name =user_permission Attributes: Id User_id Permission_id Here the user might get more permission other than the permission assigned from the group. For example, if the user has only access for reading and writes from it’s defined group then if the admin user wants then he/she can directly assign permission to access a certain menu to the specific user without assigning to the other user of the same group. I haven't seen anyone on youtube have done this stuff
@Codemycom
@Codemycom 4 года назад
Is this a school project?
@sudeeptimsina4600
@sudeeptimsina4600 4 года назад
@@Codemycom this is not school project i have been trying this stuff to do for my own but cannot carry out
@ruiyang9235
@ruiyang9235 2 года назад
Thanks a lot for the tutorial. However, I meet sqlalchemy.exe.OperationalError: (sqlite3.OperationalError) no such column when I try the same steps you showed. Could I know what is the potential thing that I missed here? Thanks a lot!
@Codemycom
@Codemycom 2 года назад
No clue, rewatch the video and try again.
@zeyx6534
@zeyx6534 2 года назад
You didn't created the database file or you made a mistake on the 'SQLALCHEMY_DATABSE_URI' line
Далее
How To Update The Database - Python and Flask #9
12:19
Databases & SQLAlchemy - Flask Tutorial Series #7
33:58
Gelik yoki Velik?
00:20
Просмотров 644 тыс.
I've been using Redis wrong this whole time...
20:53
Просмотров 351 тыс.
Python Flask development with SQLite
46:23
Просмотров 20 тыс.
Python 3.12 Generic Types Explained
18:27
Просмотров 60 тыс.
Build your first REST API with Flask and PostgreSQL
27:42
SQLAlchemy: The BEST SQL Database Library in Python
16:39
Make A Python Website As Fast As Possible!
22:21
Просмотров 679 тыс.
Gelik yoki Velik?
00:20
Просмотров 644 тыс.