How to create and show mysql database using python on CentOS and RHEL 6/7 step by step guide


How to create and show mysql database using python on CentOS and RHEL 6/7  step by step guide


In this article we will learn how to create mysql database using python language.

We need to install the mysql.connector module using the below command.

#pip3 install mysql.connector
How to create and show mysql database using python on CentOS and RHEL 6/7  step by step guide
How to create and show mysql database using python on CentOS and RHEL 6/7  step by step guide
Normally we create mysql database using “create database” statement.

How to create mysql databse using python

First we need to create one file named createdatabase.py and enter the below code into this file  .

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="abc123"
)
cur = mydb.cursor()
cur.execute("CREATE DATABASE pythonclass")

How to execute the python progam

To execute the python program we need to execute the below command.

[root@localhost ~]# python3 createdatabase.py

After executing the above command new database has been created.

Now enter the mysql promt and verify it is created or not using “show databases” statement.

How to create mysql databse using python

We also can check database list using python program.

We create one file name showdatabase.py and enter the below command.

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="abc123"
)

cur = mydb.cursor()

cur.execute("SHOW DATABASES")

for x in cur:
  print(x)


That’s all. If this article is helpful please share it!!!!




SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

1 comments: