Guest User

Untitled

a guest
Jun 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. cron_classes.py Message = ['DB connexion error!', 'Error SQL request: "'GestionBD' object has no attribute 'cursor'" : [INSERT INTO traitements (admin, date, timestamp, code_dept) VALUES (True, '2018-06-25', '2018-06-25 09:26', 84)]']
  2.  
  3. $ python
  4. Python 2.7.9 (default, Jun 29 2016, 13:08:31)
  5. [GCC 4.9.2] on linux2
  6. Type "help", "copyright", "credits" or "license" for more information.
  7. >>> import psycopg2
  8. >>> conn = psycopg2.connect("host=localhost dbname=xxx password=xxx user=xxx")
  9. >>> cur=conn.cursor()
  10. >>> cur.execute("select * from departements")
  11. >>> cur.fetchall()
  12.  
  13. .. some datas
  14.  
  15. >>> cur.execute("INSERT INTO traitements (admin, date, timestamp, code_dept) VALUES (True, '2018-06-25', '2018-06-25 14:50', 84)")
  16.  
  17. >>> conn.commit()
  18.  
  19. >>> cur.execute("select * from traitements order by date desc limit 1")
  20.  
  21. >>> cur.fetchall()
  22. [(1236, True, datetime.date(2018, 6, 25), datetime.datetime(2018, 6, 25, 14, 50), 84)]
  23.  
  24. def recordTreatment(self):
  25.  
  26. self.connectDB()
  27.  
  28. now = datetime.now()
  29.  
  30. # enregistrement de l'operation
  31. request = "INSERT INTO traitements (admin, date, timestamp, code_dept) VALUES (%s, '%s', '%s', %s)" % (self.config.MANAGER.get('admin'), self.demain.date(), now.strftime('%Y-%m-%d %H:%M'), self.config.MANAGER.get('code_dept'))
  32.  
  33. logging.info("insertTreatment request = %s" % (request,))
  34.  
  35. if not self.db.executeReq(request):
  36. self.exit('critical', self.db.messerr)
  37.  
  38. class GestionBD(object):
  39.  
  40. def __init__(self, dbname='massifs', user='postgres', passwd='postgres'):
  41.  
  42. self.params = "dbname=%s user=%s password=%s" % (dbname, user, passwd)
  43. self.messerr = None
  44.  
  45. try:
  46. self.conne = psycopg2.connect(self.params)
  47.  
  48. except psycopg2.OperationalError as err:
  49. self.messerr = 'Erreur connexion: %s' % (err, )
  50. self.echec = 1
  51.  
  52. else:
  53. self.cursor = self.conne.cursor()
  54. self.echec = 0
Add Comment
Please, Sign In to add comment