Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Getting table columns in a list
- conn = psycopg2.connect(dbname=dbname, host=host, port=port, user=user, password=pwd)
- print("Connecting to Database")
- cur = conn.cursor()
- cur.execute("SELECT * FROM " + table_name + " LIMIT 0")
- table_columns = [desc[0] for desc in cur.description]
- #print table_columns
- #Getting excel sheet columns in a list
- df = pd.read_excel('/Users/.../plans.xlsx', sheet_name='plans')
- engine = create_engine('postgresql://postgres:postgres@localhost:5432/test_db')
- column_list = df.columns.values.tolist()
- #print(column_list)
- s = set(column_list).intersection(set(table_columns))
- for x in df['column_1'] :
- sql = "insert into test_table(column_1) values ('" + x + "')"
- cur.execute(sql)
- cur.execute("commit;")
- conn.close()
Add Comment
Please, Sign In to add comment