Advertisement
Guest User

Untitled

a guest
Aug 1st, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import sys
  2. import os
  3. import cx_Oracle
  4.  
  5. class Oracle(object):
  6.  
  7. __db_server = os.getenv("ORACLE_SERVER")
  8. __db_user = os.getenv("ORACLE_ACCT")
  9. __db_password = os.getenv("ORACLE_PWD")
  10.  
  11. def connect(self):
  12. """ Connect to the database. """
  13.  
  14. try:
  15. self.db = cx_Oracle.connect(__db_user+'/'+__db_password+'@'+__db_server)
  16. except cx_Oracle.DatabaseError as e:
  17. error, = e.args
  18. if error.code == 1017:
  19. print('Please check your credentials.')
  20. else:
  21. print('Database connection error: %s'.format(e))
  22. # Very important part!
  23. raise
  24.  
  25. # If the database connection succeeded create the cursor
  26. # we-re going to use.
  27. self.cursor = db.Cursor()
  28.  
  29. def disconnect(self):
  30. """
  31. Disconnect from the database. If this fails, for instance
  32. if the connection instance doesn't exist we don't really care.
  33. """
  34.  
  35. try:
  36. self.cursor.close()
  37. self.db.close()
  38. except cx_Oracle.DatabaseError:
  39. pass
  40.  
  41. def execute(self, sql, bindvars=None, commit=False):
  42. """
  43. Execute whatever SQL statements are passed to the method;
  44. commit if specified. Do not specify fetchall() in here as
  45. the SQL statement may not be a select.
  46. bindvars is a dictionary of variables you pass to execute.
  47. """
  48.  
  49. try:
  50. self.cursor.execute(sql, bindvars)
  51. except cx_Oracle.DatabaseError as e:
  52. error, = e.args
  53. if error.code == 955:
  54. print('Table already exists')
  55. elif error.code == 1031:
  56. print("Insufficient privileges")
  57. print(error.code)
  58. print(error.message)
  59. print(error.context)
  60.  
  61. # Raise the exception.
  62. raise
  63.  
  64. # Only commit if it-s necessary.
  65. if commit:
  66. self.db.commit()
  67.  
  68. def select(self, sql, commit=False):
  69. bindvars=None
  70. result = None
  71. try:
  72. self.cursor.execute(sql, bindvars)
  73. result = self.cursor.fetchall()
  74. except cx_Oracle.DatabaseError as e:
  75. error, = e.args
  76. print "Database Error: failed with error code:%d - %s" % (error.code, error.message)
  77. raise
  78. if commit:
  79. self.db.commit()
  80. return result
  81.  
  82. def commit(self):
  83. try:
  84. self.db.commit()
  85. except cx_Oracle.DatabaseError as e:
  86. error, = e.args
  87. print "Database Commit failed with error code:%d - %s" % (error.code, error.message)
  88. raise
  89.  
  90. def rollback(self):
  91. try:
  92. self.db.rollback()
  93. except cx_Oracle.DatabaseError as e:
  94. error, = e.args
  95. print "Database Rollback failed with error code:%d - %s" %(error.code, error.message)
  96. raise
  97.  
  98. import sys
  99. import os
  100. #import cx_Oracle
  101. from Oracle import Oracle
  102.  
  103. def main():
  104. oracle = Oracle.connect()
  105. query = """SELECT DISTINCT NAME FROM MyTable"""
  106. data = oracle.select(query)
  107. for row in data:
  108. print row
  109. oracle.disconnect()
  110.  
  111. ### MAIN
  112. if __name__ == '__main__':
  113. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement