Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ Generation a Database"""
- # import the mysql client for python
- import pymysql
- def create_db():
- # Create a connection object
- database_ServerIP = "localhost" # IP address of the MySQL database server
- database_UserName = "root" # User name of the database server
- database_UserPassword = "" # Password for the database user
- database_Name = "NewDatabaseGeneric" # Name of the database that is to be created
- charSet = "utf8mb4" # Character set
- cusrorType = pymysql.cursors.DictCursor
- connectionInstance = pymysql.connect(host=database_ServerIP,
- user=database_UserName,
- password=database_UserPassword,
- charset=charSet,
- cursorclass=cusrorType)
- try:
- # Create a cursor object
- cursorInsatnce = connectionInstance.cursor()
- # SQL Statement to create a database
- sqlStatement = "CREATE DATABASE " + database_Name
- # Execute the create database SQL statment through the cursor instance
- cursorInsatnce.execute(sqlStatement)
- # SQL query string
- sqlQuery = "SHOW DATABASES"
- # Execute the sqlQuery
- cursorInsatnce.execute(sqlQuery)
- # Fetch all the rows
- databaseList = cursorInsatnce.fetchall()
- for database in databaseList:
- print(database)
- except Exception as e:
- print("Exeception occured:{}".format(e))
- finally:
- connectionInstance.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement