Created
January 11, 2018 04:06
-
-
Save kshwetabh/df21b180aafc224b8748a59e983fe578 to your computer and use it in GitHub Desktop.
Use this script to load NIFTY Intraday data into PostgreSQL Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import psycopg2 | |
conn = psycopg2.connect("host='localhost' port='5432' dbname='stocks' user='xxxxxx' password='xxxxxx'") | |
cur = conn.cursor() | |
SQL_STATEMENT = """ | |
COPY %s FROM STDIN WITH | |
CSV | |
HEADER | |
DELIMITER AS ',' | |
""" | |
def process_file(conn, table_name, file_object): | |
cursor = conn.cursor() | |
cursor.copy_expert(sql=SQL_STATEMENT % table_name, file=file_object) | |
conn.commit() | |
cursor.close() | |
f = open(r'C:\Users\xxx\Desktop\AllStocksExtracted\code\result.csv', 'r') | |
try: | |
process_file(conn, 'stocks', f) | |
finally: | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment