Guest User

Untitled

a guest
Dec 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import pymysql
  2. import random
  3.  
  4. SCHEMA = 'schema_name'
  5. TABLE_NAME = 'table_name'
  6. TABLE_FIELDS = [
  7. 'currency',
  8. 'language',
  9. ]
  10. USER = 'root'
  11. PASSWORD = 'root'
  12. CURRENCY_CODES = ['USD', 'EUR', 'IDR', 'MYR', 'BGN', 'CZK', 'DKK', 'ILS', 'INR', 'HUF', 'JPY', 'NOK', 'PLN', 'RON',
  13. 'RUB', 'CNY', 'HKD', 'TWD', 'SEK', 'THB', 'TRY', 'VND']
  14. LANGUAGES = ['de-de', 'id-id', 'ms-my', 'bg-bg', 'cs-cz', 'da-dk', 'he-il', 'hi-in', 'hu-hu', 'ja-jp', 'nb-no', 'pl-pl',
  15. 'ro-ro', 'ru-ru', 'zh-cn', 'zh-hk', 'zh-tw', 'sv-se', 'th-th', 'tr-tr', 'vi-vn']
  16. MOUNT_OF_DATA = 5 # how much rows do you need
  17.  
  18.  
  19. connection = pymysql.connect(host='localhost', user=USER, password=PASSWORD,
  20. db=SCHEMA, cursorclass=pymysql.cursors.DictCursor)
  21.  
  22.  
  23. def generate_data() -> list:
  24. generated_data = []
  25. for current_amount in range(AMOUNT_OF_DATA):
  26. # Here you need to write logic for output fields
  27. currency = random.choice(CURRENCY_CODES)
  28. language = random.choice(LANGUAGES)
  29. # You need to append tuple in the same order as in table
  30. generated_data.append((currency, language))
  31. return generated_data
  32.  
  33.  
  34. try:
  35. with connection.cursor() as cursor:
  36. sql = "INSERT IGNORE INTO {schema}.{table} ({fields}) " \
  37. "values ({values})".format(schema=SCHEMA, table=TABLE_NAME, fields=', '.join(TABLE_FIELDS),
  38. values=", ".join(["%s"] * len(TABLE_FIELDS)))
  39. cursor.executemany(sql, generate_data())
  40. connection.commit()
  41. finally:
  42. connection.close()
Add Comment
Please, Sign In to add comment