Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. import pandas as pd
  2. import psycopg2 as ps
  3.  
  4.  
  5. def checkNan(text, default, add_single_quotes=False):
  6. if text == 'nan':
  7. return str(default)
  8.  
  9. if add_single_quotes:
  10. return str('\'' + text + '\'')
  11.  
  12. return str(text)
  13.  
  14.  
  15. data = pd.read_csv('forecast.csv')
  16. delete_query = 'DELETE FROM forecast_corp;'
  17. insert_query = 'INSERT INTO forecast_corp (id, date, country, reseller_type, corp_id, corp_name, corp_size_categorization, gmv_forecast, forecast_update_created_on) VALUES \r\n'
  18.  
  19. for index, row in data.iterrows():
  20. id = str(row['id'])
  21. date = checkNan(str(row['date']), 'NULL')
  22. country = checkNan(str(row['country']), 'NULL')
  23. reseller_type = checkNan(str(row['reseller_type']), 'NULL')
  24. corp_id = checkNan(str(row['corp_id']), '0.0')
  25. corp_name = checkNan(str(row['corp_name']), 'NULL')
  26. corp_size_categorization = checkNan(str(row['corp_size_categorization']), 'NULL')
  27. gmv_forecast = str(row['gmv_forecast'])
  28. forecast_update_created_on = checkNan(str(row['forecast_update_created_on']), 'NULL', True)
  29.  
  30. insert_query += '\t(' + id + ', \'' + date + '\', \'' + country + '\', \'' + reseller_type + '\', ' + corp_id + ', \'' \
  31. + corp_name + '\', \'' + corp_size_categorization + '\', ' + gmv_forecast + ', ' + forecast_update_created_on \
  32. + '),\r\n'
  33.  
  34. insert_query = insert_query[0:len(insert_query) - 3].replace('\'NULL\'', 'NULL') + ';'
  35.  
  36. with open('query.txt', 'w') as f:
  37. f.write(insert_query)
  38.  
  39. connection = None
  40. try:
  41. connection = ps.connect(user='biuser',
  42. password='IguanaBi01',
  43. host='bi.clp7yqehsl0w.us-east-1.rds.amazonaws.com',
  44. port='5432',
  45. database='bi')
  46. delete_cursor = connection.cursor()
  47. delete_cursor.execute(delete_query)
  48.  
  49. insert_cursor = connection.cursor()
  50. insert_cursor.execute(insert_query)
  51. connection.commit()
  52.  
  53. result = insert_cursor.rowcount
  54.  
  55. if result is not None and result > 0:
  56. print('Yeah! It worked.')
  57. else:
  58. print('You suck!')
  59.  
  60. insert_cursor.close()
  61. except (Exception, ps.Error) as error:
  62. print('Error while fetching data from PostgreSQL', error)
  63. finally:
  64. # closing database connection.
  65. if connection:
  66. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement