STEM all the way!
Given so many major languages and technological concepts we’ve been through for the past few weeks, there’s one major language that is missing in our playbook – Pythion. For its rich libraries in data science, mathematical compatibility, and perfect unity with Linux, there’s no reason we shouldn’t cover the language. Here’s our first step into the Pythonic world with MySQL.
As I mentioned the MySQL database’s crucial integrity in the tech world in one of the previous posts, JDBC on Linux – Part 1: Connecting to Database, I also do some curations in other languages’ methods to interact with the database.
Since I’ve already covered a lot about major languages, including Java, C, and Kotlin, one language is missing in our playbook – Python. And given the fact that Python 3 is already installed on most recent Ubuntu versions, you really don’t have to install it by yourself if you also use the distro (of course, you need to install the database software all by yourself, though).
And, when I think about my own previous experiences right before I became a Java programmer, Python actually played a significant role to gain my current position as a software developer.
When I was at the previous work, I already started learning and experimenting with my own coding journey with Excel VBA. Considering the fact that Python had a lot of corresponding libraries that work together well with the spreadsheet software, such as Openpyxl, I couldn’t help but spent whole weekends recreating what I did with VBA with Python.
During the job interview, my personal activities with Python attracted the interviewer, and eventually, I got this job, so I have some familiarity with the data-science-oriented language. And especially considering its undeniably rich source of data-analytic libraries that its strong compatibility with mathematics, the language collides its path with so many of my interests and personal activities, including one of my series – Algebra.
So throughout this series, we’ll go through the endless world of Python and its powerful libraries that give us plenty of time to invite our inner curious child to come out and play with our technological creativity.
Here, we’ll see how to connect Python with MySQL database and do some basic queries. Let’s have some fun, shall we?
Install MySQL connector:
First things first, you need to install the MySQL Connector (I assume you already installed the database on your machine). By the way, as I mention the last time, my environment is Ubuntu Server 20.04 which is installed on my home server.
pip install mysql-connector-python
Basic connector program:
Here, this is our first Python program that connects the language with the MySQL database.
mysql_connection.py
Executed result:
Show-database program:
This is a program that executes the “show database” command on the MySQL server.
mysql_show_db.p
Executed result:
Show-tables program:
This is a program that executes the “show tables” command on the MySQL server.
mysql_show_tbls.py
Executed result:
Select-everything program:
Finally, this is a program that executes the “select * from table_name” command on the MySQL server.
mysql_select.py
Executed result:
Afterthoughts:
So, how was it? Did you like it? If you’re interested, you can do more with you and your own research. Keep learning, keep coding, and keep smiling!