Guest User

Untitled

a guest
Sep 14th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #Getting table columns in a list
  2. conn = psycopg2.connect(dbname=dbname, host=host, port=port, user=user, password=pwd)
  3. print("Connecting to Database")
  4. cur = conn.cursor()
  5. cur.execute("SELECT * FROM " + table_name + " LIMIT 0")
  6. table_columns = [desc[0] for desc in cur.description]
  7. #print table_columns
  8.  
  9. #Getting excel sheet columns in a list
  10. df = pd.read_excel('/Users/.../plans.xlsx', sheet_name='plans')
  11. engine = create_engine('postgresql://postgres:postgres@localhost:5432/test_db')
  12. column_list = df.columns.values.tolist()
  13. #print(column_list)
  14. s = set(column_list).intersection(set(table_columns))
  15. for x in df['column_1'] :
  16. sql = "insert into test_table(column_1) values ('" + x + "')"
  17. cur.execute(sql)
  18. cur.execute("commit;")
  19.  
  20. conn.close()
Add Comment
Please, Sign In to add comment