Guest User

Untitled

a guest
Jan 15th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import psycopg2
  2.  
  3. def create_table():
  4. conn=psycopg2.connect("dbname= 'database1' user='postgres' password='postgres123' host='localhost' port='5432'")
  5. cur=conn.cursor()
  6. cur.execute("CREATE TABLE IF NOT EXISTS store(item TEXT, quantity INTEGER, price REAL)")
  7. conn.commit()
  8. conn.close()
  9.  
  10. def insert(item, quantity, price):
  11. conn=psycopg2.connect("dbname= 'database1' user='postgres' password='postgres123' host='localhost' port='5432'")
  12. cur=conn.cursor()
  13. cur.execute("INSERT INTO store VALUES ('%s', '%s', '%s')" % (item,quantity,price))
  14. conn.commit()
  15. conn.close()
  16.  
  17.  
  18.  
  19. def view():
  20. conn=psycopg2.connect("lite.db")
  21. cur=conn.cursor()
  22. cur.execute("SELECT * FROM store")
  23. rows=cur.fetchall()
  24. conn.close()
  25. return rows
  26.  
  27. def delete(item):
  28. conn=psycopg2.connect("dbname= 'database1' user='postgres' password='postgres123' host='localhost' port='5432'")
  29. cur=conn.cursor()
  30. cur.execute("DELETE FROM store WHERE item=?",(item,))
  31. conn.commit()
  32. conn.close()
  33.  
  34. def update(quantity,price,item):
  35. conn=psycopg2.connect("dbname= 'database1' user='postgres' password='postgres123' host='localhost' port='5432'")
  36. cur=conn.cursor()
  37. cur.execute("UPDATE store SET quantity=?, price=? WHERE item=?",(quantity,price,item))
  38. conn.commit()
  39. conn.close()
  40.  
  41. create_table()
  42. insert("Apple", 10, 15)
Add Comment
Please, Sign In to add comment