Guest User

Untitled

a guest
Nov 20th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import psycopg2
  5. import sys
  6.  
  7.  
  8. con = None
  9.  
  10. try:
  11. con = psycopg2.connect("host='localhost' dbname='testdb' user='pythonspot' password='password'")
  12. cur = con.cursor()
  13. cur.execute("CREATE TABLE Products(Id INTEGER PRIMARY KEY, Name VARCHAR(20), Price INT)")
  14. cur.execute("INSERT INTO Products VALUES(1,'Milk',5)")
  15. cur.execute("INSERT INTO Products VALUES(2,'Sugar',7)")
  16. cur.execute("INSERT INTO Products VALUES(3,'Coffee',3)")
  17. cur.execute("INSERT INTO Products VALUES(4,'Bread',5)")
  18. cur.execute("INSERT INTO Products VALUES(5,'Oranges',3)")
  19. con.commit()
  20. except psycopg2.DatabaseError as e:
  21. if con:
  22. con.rollback()
  23.  
  24. print "Error %s % e"
  25. sys.exit(1)
  26.  
  27. finally:
  28. if con:
  29. con.close()
Add Comment
Please, Sign In to add comment