Zionetrix::Git
Repositories
Help
Report an Issue
python-mylib
Code
Commits
Branches
Tags
Search
Tree:
a3cca71
Branches
Tags
master
python-mylib
PgDB.py
Add doSQL and close methods
Benjamin Renard
commited
a3cca71
at 2015-04-01 15:52:32
PgDB.py
Blame
History
Raw
#!/usr/bin/python import psycopg2 import logging import sys class PgDB(object): host = "" user = "" pwd = "" db = "" con = 0 def __init__(self,host,user,pwd,db): self.host = host self.user = user self.pwd = pwd self.db = db def connect(self): if self.con == 0: try: con = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" % (self.db,self.user,self.host,self.pwd)) self.con = con except Exception, e: logging.fatal(e) sys.exit(1) def close(self): if self.con: self.con.close() def setEncoding(self,enc): if self.con: try: self.con.set_client_encoding(enc) return True except Exception, e: logging.error(e) return False def doSQL(self,sql,params=None): cursor = self.con.cursor() try: if params is None: cursor.execute(sql) else: cursor.execute(sql,params) self.con.commit() return True except Exception, e: logging.error('Erreur durant la requete sql %s : %s' % (sql,e)) self.con.rollback() return False def doSelect(self,sql): cursor = self.con.cursor() try: cursor.execute(sql) results = cursor.fetchall() return results except Exception, e: logging.error('Erreur durant la requete sql %s : %s' % (sql,e)) return False