Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import pandas as pd
  2. import psycopg2 as ps
  3.  
  4. data = pd.read_csv('forecast.csv')
  5. query = 'DELETE FROM forecast_corp;\r\nINSERT INTO forecast_corp (id, date, country, reseller_type, corp_id, corp_name, corp_size_categorization, gmv_forecast, forecast_update_created_on) VALUES \r\n'
  6.  
  7. for index, row in data.iterrows():
  8. query += '\t(' + str(row['id']) + ', \'' + str(row['date']) + '\', \'' + str(row['country']) +\
  9. '\', \'' + str(row['reseller_type']) + '\', ' + str(row['corp_id']) + ', \'' + str(row['corp_name']) + '\', \'' + str(row['corp_size_categorization'])+\
  10. '\', ' + str(row['gmv_forecast']) + ', '
  11.  
  12. if str(row['forecast_update_created_on']) == 'nan':
  13. query += 'NULL'
  14. else:
  15. query += '\'' + str(row['forecast_update_created_on']) + '\''
  16.  
  17. query += '),\r\n'
  18.  
  19. query = query[0:len(query)-3] + ';'
  20.  
  21. with open('query.txt', 'w') as f:
  22. f.write(query)
  23.  
  24. connection = None
  25. try:
  26. connection = ps.connect(user='biuser',
  27. password='IguanaBi01',
  28. host='bi.clp7yqehsl0w.us-east-1.rds.amazonaws.com',
  29. port='5432',
  30. database='bi')
  31. cursor = connection.cursor()
  32. cursor.execute(query)
  33. result = cursor.fetchall()
  34.  
  35. if len(result) > 0:
  36. print('Yeah! It worked.')
  37. else:
  38. print('You suck!')
  39.  
  40. cursor.close()
  41. except (Exception, ps.Error) as error:
  42. print('Error while fetching data from PostgreSQL', error)
  43. finally:
  44. # closing database connection.
  45. if connection:
  46. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement