From the course: Using SQL with Python

Unlock the full course today

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

Prepared statements

Prepared statements

- [Instructor] Prepared queries are a powerful tool in most relational database applications. Let's take a quick look at how this works. This is db_prepared.py from chapter one of the exercise files. A prepared statement is a statement that is parsed once by the database engine and then used over and over even with different values. Here, we're using MySQL and you notice I have MY_HOST, USER and PASSWORD as Constance that makes the connection line a lot cleaner. And then you notice that when I create my cursor, I have this option in here prepared=True. MySQL by default does not provide prepared statements and you always want to use prepared statements. And so by setting this prepared flag in the cursor object, you can now use prepared statements. So every time you use MySQL, you're always going to want to set prepared=True. Let's take a look at why. Down here you notice we drop the table, create the table, we're doing…

Contents