Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. import psycopg2
  2. from lxml import etree
  3.  
  4. tree = etree.parse("xmltest/rinf.xml")
  5. opname = tree.findall(".//OPName")
  6.  
  7. opnames =[]
  8. for op in opname:
  9. value = op.get("Value")
  10. opnames.append(value)
  11.  
  12. try:
  13. conn = psycopg2.connect("dbname='rinf' user='postgres' host='localhost' password='12345678' port='5432'")
  14. except:
  15. print("Connetion problem!")
  16.  
  17. cur = conn.cursor()
  18. #print(opnames)
  19. insert_query = "INSERT INTO opp (name) VALUES (%s);"
  20.  
  21. try:
  22. cur.executemany(insert_query, [(name,) for name in opnames])
  23.  
  24. except Exception as e:
  25. print("SQL command cannot be used:", e)
  26.  
  27. conn.commit()
  28. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement