Hi friends, this is Trilochan from Silan Software, BBSR. Here in this tutorial, I am presenting a simple point in python with MySQL, that is how to create a database and show all databases.
Here I have used Atom as my IDE and MySQL is my database environment. Now I will create a simple python file let demo1.py and in demo1.py we will write
the following code:
import mysql.connector con=mysql.connector.connect(host=’localhost’, user=’root’, password=’silan’) obj=con.cursor() obj.execute(“create database silandb”)
Now we will run, then a database named as silandb will create.
Here in first line we imported connector module that is mysql.connector
In second line the variable con representing establishment of connection. We are getting this by invoking connect( ) by mysql.connector module and it taking 3 arguments like host name , user value and password value of MySQL.
In third line we have created an object named as obj by invoing cursor( ) function by con.
In fourth line we invoked execute( ) function by obj and within this function we have written sql statement for creating database.
Now if we want to show databases then we will run the following code:
import mysql.connector con=mysql.connector.connect(host=’localhost’, user=’root’, password=’silan’) obj=con.cursor() obj.execute(“show databases”) for k in obj: print(k)
Now we will run the code and then open MySQL workbench, then we will execute the sql statement like;
Then we will get all databases, and among all one database named as silandb we can see.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc