Home >>Python Tutorial >Getting Started With Python MySQL

Getting Started With Python MySQL

This is the python mysql getting started part of our python MySQL tutorial. And as we have mentioned in the tutorial before, that python is a programming language that is often used in a number of database applications. And one of the most popular databases is the MySQL.

The MySQL Database

Before you go ahead and start experimenting with a number of different examples present in this python sql tutorial it is first important for you to install the MySQL software itself.

You can install MySQL on your computers. If you wish, you can also download this software for free from the site that is mentioned below.

https://www.mysql.com/downloads/  

To Install MySQL Driver

If you wish your python programming language to be able to access the MySQL connector python database then it is important for you to first possess the MySQL driver.

And in this particular tutorial, we are going to use the driver named as the ‘MySQL Connector’. It is also recommended that if you haven’t yet installed the ‘MySQL Connector’ then you should use PIP to install this particular driver.

This is because of the fact that you might already possess PIP in your python environment. And it will just be a good practice for you. So, if you wish to follow this method then you need to navigate to the command line in PIP. Once you are there you can type the example that is mentioned below.  

C: \Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>python –m pip install mysql-connector

Once you type the example that is mentioned above then you would have downloaded and installed your MySQL Connector python driver.  

To Test MySQL Connector

If you wish to check whether or not you already possess the MySQL driver or if you have installed this driver correctly then you can perform a simple test.

This test basically requires you to create a python page that has a particular content.

That particular python sql tutorial content is mentioned below. import mysql.connector You can save this as demo_mysql_test.py.

And whenever you run this demo and if no error is displayed then that indicated that the ‘MySQL Connector’ is installed and is ready for you to use.

To Create Connections

To start learning about this database, it is first important for you to know the process of creating a python database connection. You can also use your particular username and password for your MySQL database. For example

import mysql.connector



mydb = mysql.connector.connect (

  host=”localhost”,

  user=”yourusername”,

  passwd=”yourpassword”

)



print (mydb)

  Once you create this python database connection, then you can start asking queries from your database by using the specific SQL statements. With this, we finish the python mysql getting started part of our python MySQL tutorial.


No Sidebar ads