Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import psycopg2
  4.  
  5. def getconnection(dbname, user, password, host, port):
  6. try:
  7. conn = psycopg2.connect("dbname="+dbname+" user="+user+" password="+password+" host="+host+" port="+port)
  8. print('CONNECTION SUCCESSFUL')
  9. return conn
  10. except:
  11. print('CONNECTION FAILED')
  12.  
  13.  
  14. def insert(conn, sql):
  15. cur = conn.cursor()
  16. try:
  17. cur.execute(sql)
  18. conn.commit()
  19. print('INSERT SUCCESSFUL')
  20. except:
  21. print('INSERT FAILED')
  22.  
  23.  
  24. def delete(conn,sql):
  25. cur = conn.cursor()
  26. try:
  27. cur.execute(sql)
  28. conn.commit()
  29. print('DELETE SUCCESSFUL')
  30. except:
  31. print('DELETE FAILED')
  32.  
  33.  
  34. def update(conn,sql):
  35. cur = conn.cursor()
  36. try:
  37. cur.execute(sql)
  38. conn.commit()
  39. print('UPDATE SUCCESSFUL')
  40. except:
  41. print('UPDATE FAILED')
  42.  
  43.  
  44. def select(conn, sql, limit=):
  45. cur = conn.cursor()
  46. try:
  47. cur.execute(sql)
  48. print('SELECT SUCCESSFUL')
  49. rows = cur.fetchall()
  50. print('Rows: ')
  51. for row in rows:
  52. siz = len(row)
  53. for i in range(0,siz):
  54. print(row[i], end=' ')
  55. print('')
  56. except:
  57. print('SELECT FAILED')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement