Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import psycopg2 as ps
- data = pd.read_csv('forecast.csv')
- 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'
- for index, row in data.iterrows():
- query += '\t(' + str(row['id']) + ', \'' + str(row['date']) + '\', \'' + str(row['country']) +\
- '\', \'' + str(row['reseller_type']) + '\', ' + str(row['corp_id']) + ', \'' + str(row['corp_name']) + '\', \'' + str(row['corp_size_categorization'])+\
- '\', ' + str(row['gmv_forecast']) + ', '
- if str(row['forecast_update_created_on']) == 'nan':
- query += 'NULL'
- else:
- query += '\'' + str(row['forecast_update_created_on']) + '\''
- query += '),\r\n'
- query = query[0:len(query)-3] + ';'
- with open('query.txt', 'w') as f:
- f.write(query)
- connection = None
- try:
- connection = ps.connect(user='biuser',
- password='IguanaBi01',
- host='bi.clp7yqehsl0w.us-east-1.rds.amazonaws.com',
- port='5432',
- database='bi')
- cursor = connection.cursor()
- cursor.execute(query)
- result = cursor.fetchall()
- if len(result) > 0:
- print('Yeah! It worked.')
- else:
- print('You suck!')
- cursor.close()
- except (Exception, ps.Error) as error:
- print('Error while fetching data from PostgreSQL', error)
- finally:
- # closing database connection.
- if connection:
- connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement