Advertisement
Guest User

Untitled

a guest
Nov 29th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. """ Generation a Database"""
  2.  
  3. # import the mysql client for python
  4. import pymysql
  5.  
  6.  
  7. def create_db():
  8.     # Create a connection object
  9.     database_ServerIP = "localhost"  # IP address of the MySQL database server
  10.     database_UserName = "root"  # User name of the database server
  11.     database_UserPassword = ""  # Password for the database user
  12.     database_Name = "NewDatabaseGeneric"  # Name of the database that is to be created
  13.     charSet = "utf8mb4"  # Character set
  14.  
  15.     cusrorType = pymysql.cursors.DictCursor
  16.     connectionInstance = pymysql.connect(host=database_ServerIP,
  17.                                          user=database_UserName,
  18.                                          password=database_UserPassword,
  19.                                          charset=charSet,
  20.                                          cursorclass=cusrorType)
  21.     try:
  22.         # Create a cursor object
  23.         cursorInsatnce = connectionInstance.cursor()
  24.  
  25.         # SQL Statement to create a database
  26.         sqlStatement = "CREATE DATABASE " + database_Name
  27.  
  28.         # Execute the create database SQL statment through the cursor instance
  29.         cursorInsatnce.execute(sqlStatement)
  30.  
  31.         # SQL query string
  32.         sqlQuery = "SHOW DATABASES"
  33.  
  34.         # Execute the sqlQuery
  35.         cursorInsatnce.execute(sqlQuery)
  36.  
  37.         # Fetch all the rows
  38.         databaseList = cursorInsatnce.fetchall()
  39.  
  40.         for database in databaseList:
  41.             print(database)
  42.  
  43.     except Exception as e:
  44.         print("Exeception occured:{}".format(e))
  45.  
  46.     finally:
  47.         connectionInstance.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement