+

Last commit for PgDB.py: a3cca712c27c67547d450a128c233be81f664524

Add doSQL and close methods

Benjamin Renard [2015-04-01 15:52:32]
Add doSQL and close methods
  1. #!/usr/bin/python
  2.  
  3. import psycopg2
  4. import logging
  5. import sys
  6.  
  7. class PgDB(object):
  8.  
  9. host = ""
  10. user = ""
  11. pwd = ""
  12. db = ""
  13.  
  14. con = 0
  15.  
  16. def __init__(self,host,user,pwd,db):
  17. self.host = host
  18. self.user = user
  19. self.pwd = pwd
  20. self.db = db
  21.  
  22. def connect(self):
  23. if self.con == 0:
  24. try:
  25. con = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" % (self.db,self.user,self.host,self.pwd))
  26. self.con = con
  27. except Exception, e:
  28. logging.fatal(e)
  29. sys.exit(1)
  30.  
  31. def close(self):
  32. if self.con:
  33. self.con.close()
  34.  
  35. def setEncoding(self,enc):
  36. if self.con:
  37. try:
  38. self.con.set_client_encoding(enc)
  39. return True
  40. except Exception, e:
  41. logging.error(e)
  42. return False
  43.  
  44. def doSQL(self,sql,params=None):
  45. cursor = self.con.cursor()
  46. try:
  47. if params is None:
  48. cursor.execute(sql)
  49. else:
  50. cursor.execute(sql,params)
  51. self.con.commit()
  52. return True
  53. except Exception, e:
  54. logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
  55. self.con.rollback()
  56. return False
  57.  
  58. def doSelect(self,sql):
  59. cursor = self.con.cursor()
  60. try:
  61. cursor.execute(sql)
  62. results = cursor.fetchall()
  63. return results
  64. except Exception, e:
  65. logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
  66. return False