Grocery Store Application - 3. Products Backend | Python project tutorial

codebasics · Beginner ·🔧 Backend Engineering ·5y ago

Key Takeaways

The video tutorial demonstrates building a backend for a grocery store application using Python and SQL, covering topics such as database connection, modular code, and SQL queries. It utilizes tools like MySQL connector, cursor, and inner join to manage products and perform queries.

Full Transcript

now we have three layers in our application ui back-end and front-end but if you think about modules we have two major models products and orders now when the project is managed in a corporate setting they often do vertical slice development what i mean by that is they take products as a one feature so you can see this vertical slice they develop that feature fully and then release it then they pick up orders and they develop the whole vertical slice and then they release it the other approach would be you do database development for all the modules then you do back-end development for all the modules and then you do ui development for all the models the disadvantage of that approach is that if you keep your ui towards the end then it will take long time before your customer sees anything but in this case you can just build products module give it to your grocery store owner ask him to start using it so you get feedback or layer now for software project development companies use a process called scrum scrum allows you to break down and prioritize your task and it is a very very popular technique that is being used in majority of the software companies and the tool that allows you to do scrum is called jira jira is a very popular tool from atlasian that is being used in majority of the companies who are doing a scrum based project management now uh for if you want to know more about scrum then there is a there is this website called scrum training series dot com it has little videos 10 minutes 15 minutes videos these videos are awesome so you should see these videos these videos will be very helpful when you join any company as a software developer as a data scientist even in non-tech companies they use chrome for doing a project development okay now what we have we have done here is i created an empty folder called grocery app where i have created two sub folders backend and ui and these folders are empty right now this is empty i don't know why the arrow is showing up back-end will contain our python flash code ui will contain our html code so the first thing i'm going to do is i'm building a products page okay so our goal is to build this product page where you can do add new product you can list all the products so i'm going to write a file called products dao what the heck is dao it is data access object it's a common terminology that is used in the industry and let me go to my meditation my zen mode all right now i will uh i just googled it and find out uh found this uh driver for mysql so driver is basically a python module that allows you to connect to a database so what we're going to do is see in this query if you write this query it returns all the products so in my table i have inserted all these products you can just you know you can just double click here type in products click on apply it will insert the record i'm going to execute same query from python and retrieve all these products in my python code and i need this mysql connector module and to install it you can see by the way i have a github page so let me just show you so i have a github page for the grocery store app and on that i'll give an installation instruction so peep install mysql connector python that will install that module for you now first thing is how do you connect to a database so let's just copy paste this code so what we're doing is we're connecting to our database and username and password is root root what is the database name well it's grocery underscore store see whatever you have here you should type that here grocery when i run it nothing happens which means the database connection worked okay i want to now query the products okay so for that again i go back to documentation there is a code snippet for querying products so for querying products the first thing you need is cursor okay so cursor allows you to execute the query so you can say cursor.execute and query all right what is my query query is something what the heck is my query well i can copy this okay and once query is executed the results are within a cursor and you can i trade through those results so you can say for a tuple some tuple in cursor uh do something okay what is this tuple well this tuple is exactly your column name so column names are uh product id name uh let's see yeah product id name um id price per unit so i am going to do some copy paste you need to know copy paste skill in order to become a better or an effective uh software developer okay when you execute this not enough values to unpack okay i don't have a unit name actually see i got all these values i got these values but my um is still id see i don't have like a kg or each so i need that and for that you need to do a join so you need to do a join on a uom id so our um table knows see um table knows one is kg so i want to do that here so what we have to do is i will um instead of just saying star i will explicitly say what are my columns that i want so i want products of product id product or display whatever right and i'm doing inner join so from this uh and then inner join inner join uom so you are inner joining products with um table on on means which column you are in doing in a join well products dot uom id is equal to um dot um id if you do ctrl enter it will execute the query but i still don't see um because i have to specify that in select so i will say dot um name because um name is in uom table okay so i'm saying um table dot u m name okay and when i do ctrl enter see i got this perfect so all i need to do is now copy this query to my python code and so my query will be this and uh here of course in the end you have to say uom name um name so now i get ur name kg each whatever but my code is not modular i want to make it modular you know so i want to have if name is equal to mean then call some function called get all products which just retrieves me all the products okay so how about i make that one and get all products and so by the way you need to store all this in a array and return it so i will create a list and insert uh dictionary objects okay so copy paste copy paste is the mantra of my success okay and you of course need to return and right click run see my products are coming here you see all the products every product is a dictionary object so they are coming up along with uom name very good now i want to make my code even more modular see i have this fascination for modularity so i want to move my sql connection code into a separate file you know such that connection is passed as and a variable into this function okay so and connection will be passed here so this connection should be something what is that something well i will just create a file called sql connection and in this i will just say get sql connection that will be my function and i return it now if i call this function multiple times it is going to create multiple connections for that reason i will just store this in a global variable and in python if there is a global variable use underscore underscore convention and so what i'm saying is now if this is none then only create it you see if this is none then only create that variable return it so now oh you have to specify this person here and get a sql so from sql connection import okay again copy paste so this is returning your sql connection so i'll just say get sql connection here right click run works like a charm so now our code is modular it is creating sql connection in a different file and whenever you use it we need it you just pass it in a function argument okay so my all products is done so when i have this web page i will call that function on this webpage and it will list all my products how about add new product well you need to write a function to add new product okay i can write it so let's see okay so right now i have this many products okay if i want to insert a new product all i do is say insert into products okay values so here you specify column name and so column name is this and here you specify values so what are the values so say by the way i'm not specifying product id because it's an auto increment column so it's gonna increment on its own so i want to insert a new product uh let's say spinach and spinach is like a bunch so it's each so um id will be one and spinach less is 34 rupees 34.5 by the way you have to put single quote around all these values otherwise it will scream and when you control enter execute it see it inserted a product see here you see one row affected green green so when you click here i see a product so i have this query all i need to do is put this query into my python code okay so i'm gonna design a new function called this okay and in this function what is my query well my query is this so this is how you specify your value so it's called parameterized query where when you do see normally you do cursor. execute and pass query right but you need to also pass the data and data will be uh these three parameters so i learned all of this from here only saying querying the data and here's the percentage as percentage s so that's what we have here so let's say let's let's think about it like what do we want to pass as a product here well it will be a dictionary okay and the dictionary is let's say this okay cabbage i'm putting cabbage yeah uh so it's a dictionary so this product will be dictionary and here in the values you need to specify name um id price per unit so you will say product name here or rather product name um id price per unit and once it is executing uh you need to do connection dot commit and then you can return cursor dot last row id so it will return you the row id of the last row that has been inserted so if you run it so it's returning me 13 which means it inserted this row fine okay what was that okay okay cabbage see i don't have a cabbage i have only 12 rows now when i execute this query again i got my cabbage yeah hooray it's working hard to believe it's working otherwise that code first time usually doesn't work okay now let's write our last function which is a delete row and it is so simple i'm going to just copy paste so delete query is delete from products where product id is equal to whatever okay so we will delete uh cabbage so i said delete product row id for cabbage rather product id for cabbage is 13 see 13 is the product id that's what i'm specifying so product id will go here it will come here and the query will be delete from products where product id is equal to one three all right and now execute again see cabbage is gone so my delete function is also working whenever you have a question on sql query just ask google okay so you say okay sql delete query most of the time w3schools.com website will pop up they will have nice simple example with the sample query you can just copy paste it's so easy to learn this sql i hope you like this session in this session we wrote python back-end code for products in the next session we are going to write the ui code for product and will connect it with this backend code so that our product page will be fully functional i hope you're liking these videos so far if you do give it a thumbs up and share it with your friends thanks for watching

Original Description

In this python project tutorial for building grocery store application we will, 1) Build a backend code for products management 2) Write code to run SQL queries in python using mysql connector Code: https://github.com/codebasics/python_projects_grocery_webapp Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Link for english playlist: https://www.youtube.com/playlist?list=PLeo1K3hjS3uu1hh_qzBt6e379cofVD9Sb Link for hindi playlist: https://www.youtube.com/playlist?list=PLPbgcxheSpE0bAjT2NzU2crJVTwh9fhO4 Prerequisites links: python english tutorial: https://www.youtube.com/playlist?list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0 python hindi tutorial: https://www.youtube.com/playlist?list=PLPbgcxheSpE1DJKfdko58_AIZRIT0TjpO https://www.khanacademy.org/computing/computer-programming/html-css https://www.khanacademy.org/computing/computer-programming/sql Discord: https://discord.gg/r42Kbuk Instagram: https://www.instagram.com/codebasicshub/ Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub Linkedin: https://www.linkedin.com/company/codebasics/ DISCLAIMER: All opinions expressed in this video are of my own and not that of my employers'.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 0 of 60

← Previous Next →
1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

This video tutorial teaches how to build a backend for a grocery store application using Python and SQL, covering database connection, modular code, and SQL queries. It provides a comprehensive guide on managing products and performing queries. By following this tutorial, viewers can learn how to create a modular backend and use parameterized queries to add new products.

Key Takeaways
  1. Execute query on products table
  2. Perform inner join on products and uom tables
  3. Modularize code by separating SQL connection into a separate file
  4. Pass SQL connection as a variable to functions
  5. Return products as a list of dictionary objects
  6. Create a SQL connection object
  7. Design a function to add new products
  8. Use parameterized queries to add new products
  9. Commit changes to the database
  10. Return the last row ID inserted
💡 Using parameterized queries can help prevent SQL injection attacks and make the code more secure.

Related Reads

Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →