SQLAlchemy Turns Python Objects Into Database Entries

  Рет қаралды 191,032

NeuralNine

NeuralNine

Күн бұрын

Today we learn about SQLAlchemy, which is an SQL Toolkit and ORM that allows us to map Python classes to database tables.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: www.neuralnine...
💻 The Algorithm Bible Book: www.neuralnine...
👕 Programming Merch: www.neuralnine...
🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine...
📷 Instagram: / neuralnine
🐦 Twitter: / neuralnine
🤵 LinkedIn: / neuralnine
📁 GitHub: github.com/Neu...
🎙 Discord: / discord
🎵 Outro Music From: www.bensound.com/

Пікірлер: 128
@dustinrolsander8336
@dustinrolsander8336 2 жыл бұрын
Very clean and easy to understand, thanks for the video. Just a small hint: at 18:51 you couldn't see the table, bc the database windows won't refresh automatically in PyCharm. You have to click the refresh button at the top of the panel (third symbol in ur case). Then it will show the additionally created tables.
@bundesdavid
@bundesdavid Жыл бұрын
Thank you very much for the help! :^) At 14:00 you could also use results = session.query(Person).filter(Person.lastname == "Smith").all() with the .all() at the end you select every element fitting to the filter!
@joxa6119
@joxa6119 Жыл бұрын
Life saver!
@kharutyunyan82
@kharutyunyan82 2 жыл бұрын
One of the best channels in youtube! Thanks for your work!!!
@stevekruger7807
@stevekruger7807 11 ай бұрын
Thanks! Very clear, easy to follow, learned enough to get me started!
@abigaildawson_dev
@abigaildawson_dev Жыл бұрын
At 13:55, you can add .all() at the end to return a list of the results, instead of iterating over them and printing: results = session.query(Person).filter(Person.lastname == "Blue").all()
@Cire-my6ur
@Cire-my6ur Жыл бұрын
1:09 this is exactly what I was looking for. THANK YOU!
@rogueb1t
@rogueb1t 2 жыл бұрын
Ive watched this video sooooo many times! Brilliant tutorial on using SQLAlchemy to abstract SQL from python. I also loved your CLASSES video! Its helped me develop my code so much faster. Cheers dude!"
@radio_electronics4047
@radio_electronics4047 Жыл бұрын
One of the imports used is deprecated. For SQLAlchemy 2.0.10 the import for the declarative_base needs to look like this: from sqlalchemy.orm import declarative_base
@Melukote_Sriharsha
@Melukote_Sriharsha Жыл бұрын
Thanks
@devataamanushya8857
@devataamanushya8857 3 ай бұрын
​@@Melukote_Sriharshaಯಾರು ಗುರು ನೀನು. ಮೇಲ್ಕೋಟೆ ಅವರ? ನಾನು ಕೂಡ
@janHodle
@janHodle Жыл бұрын
Isn‘t the use of Column legacy? I know this video is 8 months old. I think a better way is to use: first_name: Mapped[str] = mapped_column(String, nullable=True)…. If the information are optional one can use Optional from the typing library
@benshapiro9731
@benshapiro9731 Жыл бұрын
Hey man solid video, but should add a note that you are, in fact, using the legacy (albeit backward compatible) syntax here. Mapped_column() is now the new factory function for the Column() class
@reginaldmaisonneuve7594
@reginaldmaisonneuve7594 Жыл бұрын
Nicely done. Simple and effective example that immediately clarified proper structure.
@juliusdgenius1
@juliusdgenius1 Жыл бұрын
Clear and informative video. I have just started to learn orm and sqlalchemy and this video is superb. Thanks for sharing.
@aszx-tv4pq
@aszx-tv4pq 7 ай бұрын
superb I'm gonna present this in my advanced python class for my classmates.
@Troteos
@Troteos Жыл бұрын
Another great video. You are such a good teacher, while your style of explaining things seems so effortless.
@jmxt3
@jmxt3 5 ай бұрын
The best tutorial about SQLAlchemy. Thanks
@spanish360
@spanish360 Жыл бұрын
So with this I am now writing data from a PLC to a database to keep records for longer periods of time with out having to hold the data or do the logic inside of the PLC, thanks so much!
@pedromarques5510
@pedromarques5510 Жыл бұрын
hey, i am very curious about your work. i used to work with plcs but only assembly/wiring... i am now studying cs. could u please share your foundations? some key course maybe. thanks in advance. (can be in spanish, gracias)
@vipulkumar2845
@vipulkumar2845 8 ай бұрын
damn, you're tutorials are way too advanced, Lots of love man!!
@rpf23543
@rpf23543 Жыл бұрын
Very great article. Short, distinct on the point - Thank you!
@RalphVanSchaik
@RalphVanSchaik 3 ай бұрын
Great. Simple and clear. Thank you very much !
@NeuralNine
@NeuralNine 3 ай бұрын
Thanks!
@bhavyasingh7984
@bhavyasingh7984 7 күн бұрын
Thank you so much!! Very easy explaination
@michaelnockov2096
@michaelnockov2096 Жыл бұрын
Thank you for this video, I was looking for soft introduction to sqlalchemy. Their documentation is quite difficult and their quick start actually don’t show the easiest ways to use it
@JLSXMK8
@JLSXMK8 2 жыл бұрын
This would also be awesome for teaching/learning SQL. You can tell SQLAlchemy what you want to do, and use the output to learn how to do it in SQL.
@corazondepapel9346
@corazondepapel9346 2 жыл бұрын
Bruh, I had no idea you could do this with SQLAlchemy. I use SQL a lot and always hated writing qries on Python
@fexofenadinaGenerica
@fexofenadinaGenerica 2 жыл бұрын
There are other alternatives to sqlalchemy too, like the peewee orm which is very nice as well
@sivajyothichandra
@sivajyothichandra 8 ай бұрын
In the last query execution it results out the object of respective classes. In the iteration of the loop, print(r) displayed both classes __repr__ method string format
@andymandrill
@andymandrill 2 жыл бұрын
This has been an awesome tutorial TY, I learned enough to continue my project ! Geatings from colombia !
@ootbinc
@ootbinc 2 жыл бұрын
Great video! I currently create and update SQL rows by calling stored procedures. I want to give SQLAlchemy a try.
@Nithishraj-dr4yr
@Nithishraj-dr4yr 2 жыл бұрын
stright to the point..awesome man
@ThatGuyEster
@ThatGuyEster Жыл бұрын
This is a treasure! Thank you so much!
@aleksalukic1904
@aleksalukic1904 2 жыл бұрын
this video has saved me a lot of time. thanks for your work :*
@sinkingboat101
@sinkingboat101 Жыл бұрын
Question: At around 7:00 the variable "firstname" defined inside the class is also defined as a variable in the constructor __init__ method. Usually, a class attribute can be called with self. ... , so is the init method overwriting the column definition or is the self.firstname call non-unique?
@benshapiro9731
@benshapiro9731 Жыл бұрын
In python there is a distinction between and instance attribute and class attribute. The class attribute is in the class’s __dict__ while the instance attr is in the instance’s __dict__. Sql alchemy’s major flaw is that it relies on this approach instead of a more pythonic one
@Mustafa-qs2rq
@Mustafa-qs2rq Жыл бұрын
This was very well explained.
@Sir-Ahmad-Khan
@Sir-Ahmad-Khan 2 жыл бұрын
Actually the reason why people call it sequel instead of SQL is because at first it was SEQL Structured English Query Language but then they skiped the E.
@sandhiyasuresh2275
@sandhiyasuresh2275 Жыл бұрын
really great session , everything is clear and easy to understand. thank you...
@andreasfloter3904
@andreasfloter3904 Жыл бұрын
Your video gives a good introduction to SQLalchemy. I would like to make one comment though. As far as I know best practice for table creation is to use singular nouns and not pural names. It would be also easier if the object name and the table name is the same. So in your example it would have been better e.g. to use for class Person __tablename__ = "person".
@MarkGithire
@MarkGithire 4 ай бұрын
This guy looks like he just escaped prison with Mike Scoffield😂😂 good work though
@mikedurham4448
@mikedurham4448 Жыл бұрын
this alchemy is the dope for us olderfarts trying to createwhile learning something to use -and hopefully rebuild some monstrosity i built in excel way back ..lol.. +
@toobeeus4648
@toobeeus4648 Жыл бұрын
Thanks. That helped a lot!
@piotrmazgaj
@piotrmazgaj 2 жыл бұрын
Uuuuu... exactly what I was looking for! Thanks!
@chriskeo392
@chriskeo392 2 жыл бұрын
plenty of these on KZbin already
@piotrmazgaj
@piotrmazgaj 2 жыл бұрын
@@chriskeo392 not on SQL Alchemy. Not in good english,
@piotrmazgaj
@piotrmazgaj 2 жыл бұрын
@@chriskeo392 not in GOOD english, read carefully please.
@chriskeo392
@chriskeo392 2 жыл бұрын
@@piotrmazgaj I think you need to look up English tutorials instead 🤡
@beyonddnoyeb4519
@beyonddnoyeb4519 3 ай бұрын
Appreciate it, Thanks!
@nujanai
@nujanai Жыл бұрын
Excellent tutorial. Thank you for the content.
@PetrokartonSpb
@PetrokartonSpb Жыл бұрын
from sqlalchemy import inspect inspector = inspect(engine) if not inspector.has_table('DB'): Base.metadata.create_all(engine)
@chikkivenu6632
@chikkivenu6632 Жыл бұрын
Quick Question: How can we have multiple databases made and used? reason for asking is to create the microservice architecture environment.
@DimitrisKougioumtzis
@DimitrisKougioumtzis 8 ай бұрын
very helpull thanks a lot but in the constructor of the object i use def __init__(self,**kwargs): self.__dict__.update((k, v) for k, v in kwargs.items()) when an object contains a lot of attributes
@alefalfa
@alefalfa Жыл бұрын
thanks. fantastic video!
@nickmills8476
@nickmills8476 Жыл бұрын
Nice video! The model constructors are generated automatically by sqlalchemy.
@pythonwriter8233
@pythonwriter8233 Жыл бұрын
Informative thanks!
@bakalmode5903
@bakalmode5903 2 жыл бұрын
great video keep it up!
@A.Mercier
@A.Mercier Жыл бұрын
re-writing __init__ function is useless if you don't implement custom behaviors within it and is only prone to raise errors
@lovemich4237
@lovemich4237 Жыл бұрын
Thank you man.
@patinronin2991
@patinronin2991 Жыл бұрын
Hello, i have the same problem like you, when i add a table the base.metadata.create_all() dosnt create the table, how to solve that ? In a poppulated database is not a option to delete the db, i used alembic but some times dosnt generate the revision file with the changes.
@HammanDauda-iu6tt
@HammanDauda-iu6tt 7 ай бұрын
Where can I find the list of all the plugins you are using?
@Somish3
@Somish3 Жыл бұрын
what if I have 5000 entries to insert? Is there any batch way, like a loop to read data from csv and add to the SQL DB or any other less manual way?
@MichaelDeBusk
@MichaelDeBusk 2 жыл бұрын
"Persons" is indeed a valid English word, and would be the more appropriate choice for the database.
@surajprasad118
@surajprasad118 Жыл бұрын
Brilliant video Thank You Sir :) Can I do the same with an auto increment column ? Thanks
@A.Mercier
@A.Mercier Жыл бұрын
I think you just have to add autoincrement attribute when specifying the id column: eg: id = Column(Integer, primary_key=True, autoincrement=True)
@machoToni
@machoToni Жыл бұрын
Nice and simple
@MeetRaval-m1e
@MeetRaval-m1e Жыл бұрын
Great Help
@funy0n583
@funy0n583 2 жыл бұрын
If anybody is getting a warning saying: "Deprecated API features detected! These feature(s) are not compatible with SQLAlchemy 2.0." you can fix it by changing "from sqlalchemy.ext.declarative import declarative_base" to "from sqlalchemy.orm import declarative_base"
@stardustsong1680
@stardustsong1680 2 жыл бұрын
Maybe you should use Jupyter notebook to avoid deleting the database every time running the code.
@nocopyrightgameplaystockvi231
@nocopyrightgameplaystockvi231 2 жыл бұрын
I am thinking of making a similar module for my own project.
@mouhamedtoure2800
@mouhamedtoure2800 Жыл бұрын
How do you configure pycharm and sqlserver to follow this tutotrial?
@blackdragon5728
@blackdragon5728 Жыл бұрын
UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. MD_agric_df = pd.read_sql_query(text(sql_query), connection) i got this error guys hope someone help ASAP in my jupyter notebook please
@princescadelpe
@princescadelpe 2 жыл бұрын
Can we create an engine if are database user only has Create, Drop, Delete permissions, or does our database user need to have All permissions set up?
@al-edrisy1129
@al-edrisy1129 Жыл бұрын
sorry but how did you open the database in pycharm ?
@bradzoltick6465
@bradzoltick6465 Жыл бұрын
Thank you.
@dmitrykononenko6411
@dmitrykononenko6411 2 жыл бұрын
Is it possible to use sqlalchemy for more advanced sql queries? For example, I mean joining, subqueries or CVEs. Also how to do queries on views?
@konjikiyami7301
@konjikiyami7301 2 жыл бұрын
Yes, you can use SQLAlchemy to perform advanced queries, and you can further customize the Python models, relationships, etc. If you find that the syntax of a query gets challenging in SQLAlchemy (has happened to me a few times), it also provides methods for you to be able to write the raw SQL yourself, and have the engine execute the query. I generally just wrote raw SQL when having to query views, though I admit most of my usecases did not need views, so I never dove too far with them.
@bakasenpaidesu
@bakasenpaidesu 2 жыл бұрын
Need SQLModel tutorial becz itz hard to find... the one already exist doesn't help me at all ...
@m_d_cert
@m_d_cert Жыл бұрын
Thank you bro
@surajpatil8670
@surajpatil8670 Жыл бұрын
Which plugin did you use to directly view .db file in Pycharm?
@NeuralNine
@NeuralNine Жыл бұрын
It is a feature of PyCharm. If it does not work for you, maybe it is only available in the Professional edition.
@j.k.notrowling2449
@j.k.notrowling2449 Жыл бұрын
Could you share the code you write in the tutorial?
@ChristopherBruns-o7o
@ChristopherBruns-o7o 7 ай бұрын
ever wonder if "f" confuses python. Because f strings?
@codebusters007
@codebusters007 Жыл бұрын
This is a really helpful video! Do you use some sort of a plugin to view the database inside PyCharm? :)
@dijla_younis
@dijla_younis Жыл бұрын
On vs code i use sqlite viewer to view the database
@sonblue34
@sonblue34 2 жыл бұрын
In this, you are creating your Engine in one file What am I to do if I have Classes in separate files? I assume I'm not to make multiple Engines for each Class and I'm not entirely sure how I make one engine for everything outside of the scope of the single file. I intend to use the async variation of this and multiple users will be potentially calling things.
@tim.martin
@tim.martin Жыл бұрын
Create the engine and session, create functions returning pointers to each (eg, GetEngine()) , save as "db . py". In another file: import db import db engine = db.GetEngine() session = db.GetSession()
@premmanu6557
@premmanu6557 2 жыл бұрын
Can you do a vid on how to build a neural network to extract data from a lisence
@patinronin2991
@patinronin2991 Жыл бұрын
You can use object character recognition (ocr) i don't remember what's the name of the package in python
@MartinSh429
@MartinSh429 2 жыл бұрын
I work with pycharm for my classes, but never tried to create a database in pycharm. is that something that I have to add to pycharm? I have to use to PgAdmin everysingle time and use postgres
@MartinSh429
@MartinSh429 2 жыл бұрын
figured it out finally!
@bennet5467
@bennet5467 Жыл бұрын
good stuff
@powlock1994
@powlock1994 2 жыл бұрын
Hello, is there any better way to create a table with hundreds of columns, instead of writing them by hands as arguments in the class? Thanks Regards
@robertstanton4694
@robertstanton4694 Жыл бұрын
Where do you have the data containing hundreds of columns currently?
@christiankoch4627
@christiankoch4627 2 жыл бұрын
I prefer querys it is faster for large and complex databases
@RobdarkRobdark
@RobdarkRobdark Жыл бұрын
Can I use the dataclasses to simplify the class definition ?
@patinronin2991
@patinronin2991 Жыл бұрын
Yes You can do it
@snippletrap
@snippletrap Жыл бұрын
"persons" is valid English, just a bit unusual
@silentgaming678
@silentgaming678 Жыл бұрын
What is the use of the representation function i did not understand. Can anyone explain?
@patinronin2991
@patinronin2991 Жыл бұрын
When You print a object in python it prints in the screen the result of rep method, You can Overdrive this method inside your class to print what You want.
@smenezes5359
@smenezes5359 4 ай бұрын
This seems more complicated than SQL
@Sri.k_Codes
@Sri.k_Codes 12 күн бұрын
I think using SQL queries are easier than this ORM thing
@BathingAfrican
@BathingAfrican 2 жыл бұрын
could you do a odoo tutorial?
@igorgrbaslijev8593
@igorgrbaslijev8593 Жыл бұрын
SQL - Squeel
@SkyFly19853
@SkyFly19853 2 жыл бұрын
Can it be used for video game development in Python?
@ptichiy-krik
@ptichiy-krik 2 жыл бұрын
Yes, of course. You can use this for an easier way to read and write persistent data. Fast-changing data, such as the position of an opponent, is stored in RAM, while slower-changing and longer-term data, such as a high score table, is stored in a database. To work with the database, you can use SQAlchemy
@SkyFly19853
@SkyFly19853 2 жыл бұрын
@@ptichiy-krik And that's what I am interested in. Thank you for your explanation.
@fexofenadinaGenerica
@fexofenadinaGenerica 2 жыл бұрын
Nice!
@aswathvanan3833
@aswathvanan3833 2 жыл бұрын
can you use it with routes
@astianmuchui
@astianmuchui Жыл бұрын
Perfect
@akartaltepe
@akartaltepe Жыл бұрын
Is sqlalchemy usable with @dataclass?
@patinronin2991
@patinronin2991 Жыл бұрын
Yes, You can use it
@محمودبوحسين-ص1ج
@محمودبوحسين-ص1ج Жыл бұрын
great
2 жыл бұрын
thx
@lensofgrace3976
@lensofgrace3976 6 ай бұрын
Squil!😅
@robertmazurowski5974
@robertmazurowski5974 Жыл бұрын
Can you say squeal?
@Bryce_Franzen
@Bryce_Franzen 14 күн бұрын
It’s actually pronounced sqalchemy
@funbionaeko
@funbionaeko Жыл бұрын
persons is english by the way
@matthiasbirling6141
@matthiasbirling6141 Жыл бұрын
Did you just assume Anna Blue's gender
@erickruhcardozo
@erickruhcardozo 5 ай бұрын
The confusion is so visible when you know nothing about what you're talking. Kinky....
@philtoa334
@philtoa334 Жыл бұрын
Thx_.
@richardmohan1037
@richardmohan1037 2 жыл бұрын
First
@Amor0913
@Amor0913 2 ай бұрын
Thanks for this video!
SQLAlchemy Makes Using Python Databases EASY
19:08
Eric Roby
Рет қаралды 9 М.
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 307 М.
New Colour Match Puzzle Challenge With Squid Game 2 - Incredibox Sprunki
00:30
I didn’t expect that #kindness #help #respect #heroic #leohoangviet
00:19
1%🪫vs 100%🔋
00:36
Аришнев
Рет қаралды 3,3 МЛН
How Strong is Glass? 💪
00:25
Brianna
Рет қаралды 29 МЛН
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,3 МЛН
SQLAlchemy: The BEST SQL Database Library in Python
16:39
ArjanCodes
Рет қаралды 99 М.
I tried 8 different Postgres ORMs
9:46
Beyond Fireship
Рет қаралды 450 М.
Raw SQL, SQL Query Builder, or ORM?
16:19
ArjanCodes
Рет қаралды 108 М.
Inside the V3 Nazi Super Gun
19:52
Blue Paw Print
Рет қаралды 3,3 МЛН
Python SQL Alchemy: Never Write SQL Syntax Again
49:48
Code with Josh
Рет қаралды 4,4 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50
C can do this too and it's faster than Python
2:09:48
Tsoding Daily
Рет қаралды 24 М.
PostgreSQL in Python - Crash Course
16:06
NeuralNine
Рет қаралды 92 М.
Front-end web development is changing, quickly
3:43
Fireship
Рет қаралды 1,4 МЛН
New Colour Match Puzzle Challenge With Squid Game 2 - Incredibox Sprunki
00:30