+

Last commit for MyDB.py: 9ba2a43a04a3ad2ea65478ba1cbd1b715ad771bc

Initial commit

Benjamin Renard [2013-06-07 10:13:03]
Initial commit
  1. #!/usr/bin/python
  2.  
  3. import MySQLdb
  4. import logging
  5. import sys
  6.  
  7. class MyDB(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 = MySQLdb.connect(self.host,self.user,self.pwd,self.db)
  26. self.con = con
  27. except Exception, e:
  28. logging.fatal(e)
  29. sys.exit(1)
  30.  
  31. def doSQL(self,sql):
  32. cursor = self.con.cursor()
  33. try:
  34. cursor.execute(sql)
  35. self.con.commit()
  36. return True
  37. except Exception, e:
  38. logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
  39. self.con.rollback()
  40. return False
  41.  
  42. def doSelect(self,sql):
  43. cursor = self.con.cursor()
  44. try:
  45. cursor.execute(sql)
  46. results = cursor.fetchall()
  47. return results
  48. ret=[]
  49. t=0
  50. for row in results:
  51. c=0
  52. for field in row:
  53. ret[t][c]=field
  54. c=c+1
  55. t=t+1
  56. return ret
  57. except Exception, e:
  58. logging.error('Erreur durant la requete sql %s : %s' % (sql,e))
  59. return False