From the course: Advanced Python: Working with Databases
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Interacting with a Postgres database using Python - Python Tutorial
From the course: Advanced Python: Working with Databases
Interacting with a Postgres database using Python
- [Instructor] Let's create a function that inserts a new sale into our Postgres database with dynamic data. Like in the other case with MySQL, we'll pass the cursor in, but you could pass in the database connection instead. Inside this function, we'll calculate the order total. You might want to break this order total calculation into its own function to encourage a single responsibility design and break apart the business logic more. For now, we'll just put it here. The order total will be the quantity multiplied by the price. We'll also apply the discount if there is one. Now let's insert the item. We could use the same methodology as in the last video where we created a tuple and then inserted it, but this time we'll use named placeholders. We'll create a dictionary and a set of key value pairs. We'll call this our sale data. Then we'll pass this data to a SQL statement. Here, each key in our query will be replaced…
Contents
-
-
-
-
-
-
What is PostgreSQL?2m 37s
-
(Locked)
Creating a PostgreSQL database2m 20s
-
(Locked)
Creating a table in Postgres using Python2m 17s
-
(Locked)
Inserting data into a Postgres database2m 6s
-
(Locked)
Interacting with a Postgres database using Python3m 40s
-
(Locked)
Setting up SQLAlchemy Core to connect to a Postgres database2m 51s
-
(Locked)
Manipulating with Postgres data using SQLAlchemy Core2m 50s
-
(Locked)
Setting up SQLAlchemy ORM to connect to a Postgres database1m 21s
-
(Locked)
Manipulating with Postgres data using SQLAlchemy ORM2m 51s
-
(Locked)
Challenge: Create a Postgres database2m 7s
-
(Locked)
Solution: Create a Postgres database6m 41s
-
-