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

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