databaseconn.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/python2.7
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Sat Jul 28 12:34:00 2018
  5. @author: dufs
  6. """
  7. import os
  8. import cx_Oracle
  9. import logging
  10. import logging.config
  11. logging.config.fileConfig('conf/logging.conf')
  12. logger = logging.getLogger(__name__)
  13. class DataBaseConn():
  14. '''
  15. '''
  16. def __init__(self, url, nls_lang='SIMPLIFIED CHINESE_CHINA.UTF8'):
  17. self.url=url
  18. try:
  19. os.environ['NLS_LANG'] = nls_lang
  20. self.conn=cx_Oracle.connect(self.url)
  21. except Exception as e:
  22. logger.error('get connect faild : ' + str(e) , exc_info=True)
  23. raise
  24. def close_connect(self):
  25. self.conn.close()
  26. def exec_sql(self, sql, *argList):
  27. # sql
  28. try:
  29. if not isinstance(sql,basestring):
  30. logger.error('sql is not a string ', exc_info=True)
  31. raise
  32. else:
  33. #logger.info('get exec sql = %s ' , sql)
  34. self.curs=self.conn.cursor()
  35. if argList:
  36. logger.debug('get exec sql argList : %s', str(argList))
  37. result=self.curs.execute(sql, argList[0])
  38. else:
  39. result=self.curs.execute(sql)
  40. self.conn.commit()
  41. #logger.info('exec sql success')
  42. return result
  43. except Exception:
  44. logger.error('execInsertSql faild : ' , exc_info=True)
  45. raise