Inserting Data Into MySQL Using Python

July 20, 2023

One thing I absolutely hate about SQL is how annoying it can be to insert new rows into a table.

My problem isn’t with the syntax.

That’s simple to understand. Doesn’t require much intuition.

But let’s say you wanna add 100 or more rows to a table. Now we’ve got some problems. You can’t call the usual INSERT INTO query and be happy with rewriting that over and over and over again.

So what do you do?

You call on Python for some help.

Depending on the type of data you want to insert into your table, Python can make that process easy.

Take a look at the code I wrote below…

Python code to insert values into MySQL

While transferring data from a web scraper to a MySQL database, I didn’t have to write an INSERT INTO query hundreds of times. Instead, I used an easy-to-use library and a basic f-string.

All I needed after this was a few more lines of code to connect Python to MySQL and voila…

We’re done.

Here’s why this matters to you…

For most people working with SQL, Python isn’t the most essential skill to have with it. But like with most other software, there are ways to integrate Python with what you’re doing to automate tasks and make your life easier.

I don’t know about you, but I like easy work.

And Python is one of the best ways to make tough work easy.