Discuss Python MySQL

Vijay Parekh

6 months ago

Python MySQL | Insideaiml
Table of Contents
  • Introduction
  • Installing MySQL
  • Steps
  • Connecting to MySQL Database
  • Creating Databases
  • Creating a cursor object and Execute the query

Introduction

          Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985-1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). This tutorial gives enough understanding on Python programming language. This tutorial explains how to communicate with MySQL database in detail, along with examples.

Installing MySQL

          Download and install MySQL from the MySQL official website. You need to install the MySQL server to follow this tutorial. Then you need to install mysql.connector for Python. We need mysql.connector to connect Python Script to the MySQL database. mysql.connector from here and install it on your computer. 

Steps

         There are the following steps to connect a python application to our database.
  • Import mysql.connector module
  • Create the connection object.
  • Create the cursor object
  • Execute the query

Connecting to MySQL Database

          The connect () method of the mysql.connector module is used to establish a connection between the MySQL database and the Python application. Pass the database details such as hostname, username and password for the database in the method call. The method returns the connection object. .
import mysql.connector as mysql

test= mysql.connect(
  host="localhost",
  user="username",
  password="password"
)

print(test)
Output
>python demo_mysql_connection.py
<mysql.connector.connection.MySQLConnection object ar 0x016645F0>

Creating Databases

          To create a database in MySQL, we use CREATE DATABASE database_name statement.
import mysql.connector as mysql

test= mysql.connect(
  host="localhost",
  user="username",
  password="password"
)

mycursor = test.cursor()

mycursor.execute("CREATE DATABASE mydatabase")

Creating a cursor object and Execute the query

         The cursor object can be defined as an abstraction specified in Python DBAPI 2. It makes it easier for us to have several separate working environments through the same connection to the database. We can create the cursor object by using the 'cursor' function of the connection object. The cursor object is an important aspect when executing database queries.
import mysql.connector as mysql

test= mysql.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword"
)

mycursor = test.cursor()

mycursor.execute("SHOW DATABASES")

for x in mycursor:
  print(x)
Output
('information_scheme',)
('mydatabase',)
('performance_schema',)
('sys',)
Enjoyed reading this blog? Then why not share it with others. Help us make this AI community stronger. 
To learn more about such concepts related to Artificial Intelligence, visit our insideAIML blog page.
You can also ask direct queries related to Artificial Intelligence, Deep Learning, Data Science and Machine Learning on our live insideAIML discussion forum.
Keep Learning. Keep Growing.

Submit Review