Python MySQL Database¶
SQL stands for Structured Query Language and is a widely used programming language for managing relational databases. Python can be used in database applications. One of the most popular databases is MySQL. Python needs a MySQL driver to access the MySQL database.
- pip install mysql-connector-python
To be able to experiment with the code examples in this course, you should have MySQL installed on your computer.
You can download XAMPP a free MySQL database at https://www.apachefriends.org/download.html. After download the XAMPP Start Apache and MySQL Server.
Note: If the above code was executed with no errors, “MySQL Connector” is installed and ready to be used.
Establishing a Connection With MySQL Server¶
MySQL is a server-based database management system. One server might contain multiple databases. To interact with a database, you must first establish a connection with the server. The general workflow of a Python program that interacts with a MySQL-based database is as follows:
Connect to the MySQL server.¶
- Create a new database.
- Connect to the newly created or an existing database.
- Execute a SQL query and fetch results.
- Inform the database if any changes are made to a table.
- Close the connection to the MySQL server.