Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. # import the mysql client for python
  2.  
  3. import pymysql
  4.  
  5. # Create a connection object
  6.  
  7. databaseServerIP = "localhost" # IP address of the MySQL database server
  8.  
  9. databaseUserName = "root" # User name of the database server
  10.  
  11. databaseUserPassword = "" # Password for the database user
  12.  
  13. newDatabaseName = "testdatabase" # Name of the database that is to be created
  14.  
  15. charSet = "utf8mb4" # Character set
  16.  
  17. cusrorType = pymysql.cursors.DictCursor
  18.  
  19. connectionInstance = pymysql.connect(host=databaseServerIP, user=databaseUserName, password=databaseUserPassword,
  20.  
  21. charset=charSet, cursorclass=cusrorType)
  22.  
  23. def CreateDatabase():
  24. try:
  25. # Create a cursor object
  26. cursorInstance = connectionInstance.cursor()
  27.  
  28. # SQL Statement to create a database
  29. sqlStatement = "CREATE DATABASE " + newDatabaseName
  30.  
  31. # Execute the create database SQL statment through the cursor instance
  32. cursorInstance.execute(sqlStatement)
  33.  
  34. # SQL query string
  35. sqlQuery = "SHOW DATABASES"
  36.  
  37. # Execute the sqlQuery
  38. cursorInstance.execute(sqlQuery)
  39.  
  40. # Fetch all the rows
  41. databaseList = cursorInstance.fetchall()
  42.  
  43. for datatbase in databaseList:
  44. print(datatbase)
  45.  
  46. except Exception as e:
  47.  
  48. print("Exeception occured:{}".format(e))
  49.  
  50. finally:
  51.  
  52. connectionInstance.close()
  53.  
  54.  
  55. def FillTable(database, table, data, querysize):
  56. try:
  57. print("FillTable called")
  58. # Create a cursor object
  59. cursorInstance = connectionInstance.cursor()
  60.  
  61. # SQL Statement to create a database
  62. #sqlStatement = "INSERT INTO weatherdata.testtable VALUES (1,2,3,4) "
  63. #sqlStatement ="INSERT INTO weather.Element VALUES(0,'PRCP'),(1,'SNOW'),(2,'SNWD'),(3,'TMAX')"
  64.  
  65. # Execute the create database SQL statment through the cursor instance
  66. #cursorInstance.execute(sqlStatement)
  67. import math
  68. batches = int(math.ceil(len(data) / querysize))
  69. print(batches)
  70. for i in range(batches):
  71. thisStatement = ""
  72. for z in range(querysize):
  73. if len(data) > 0:
  74. thisStatement = thisStatement + "(" + str(data[0][0]) + ",'" + str(data[0][1]) + "'),"
  75. del data[0]
  76.  
  77. if thisStatement[-1] == ",":
  78. thisStatement = thisStatement[:-1]
  79.  
  80. sqlStatement = "INSERT INTO " + database + "." + table + " VALUES " + thisStatement
  81. #sqlStatement = str(sqlStatement)
  82. print(sqlStatement)
  83. cursorInstance.execute(sqlStatement)
  84.  
  85.  
  86.  
  87. except Exception as e:
  88.  
  89. print("Exeception occured:{}".format(e))
  90.  
  91. finally:
  92.  
  93. connectionInstance.close()
  94.  
  95.  
  96. #CreateDatabase()
  97. lotsofdata = []
  98. q = 0
  99. for i in range(1000000):
  100. thisdata = []
  101. thisdata.append(1)
  102. if q == 0:
  103. thisdata.append("PRCP")
  104. q += 1
  105. elif q == 1:
  106. thisdata.append("SNOW")
  107. q += 1
  108. elif q == 2:
  109. thisdata.append("SNWD")
  110. q += 1
  111. else:
  112. thisdata.append("TMAX")
  113. q = 0
  114. lotsofdata.append(thisdata)
  115.  
  116. print(len(lotsofdata))
  117. FillTable("weather", "element", lotsofdata, 400000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement