From the course: Using SQL with Python

Unlock the full course today

Join today to access over 23,300 courses taught by industry experts.

Solution

Solution

(upbeat music) - Here's my solution for the chapter one challenge. This is 01-solution1.py from chapter one of the exercise files. Up the top here, I import my modules. I set up my constants for my SQL inside of main. You notice I define my database and cursor objects outside of the tri blocks. This is just good practice. It works if you do it inside the tri blocks, but Python will throw warnings about it. So this is just good practice. First, we connect to SQLI three. You notice that we have the path to the database. We get the version, we report the version. We set up the cursor, of course, and then we connect to my SQL, same thing. Notice the prepared = true. We always want to do that. And then we create and populate the My SQL table. So I drop table if exists. I create the table, I've got a bunch of row data here and, you know, so I'm using a prepared statement with execute many for my row data. And that works just…

Contents