By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

cx_oracle capture oracle progress messages

P: 2
Is it possible, using cx_Oracle to capture the simple messages from oracle, for example, if I am running an insert, in most Oracle tools, it would say '100 rows inserted'. I would like to capture and print these messages in my python program. TIA.
Nov 28 '06 #1
Share this Question
Share on Google+
1 Reply


bartonc
Expert 5K+
P: 6,596
Is it possible, using cx_Oracle to capture the simple messages from oracle, for example, if I am running an insert, in most Oracle tools, it would say '100 rows inserted'. I would like to capture and print these messages in my python program. TIA.
I had the same problem with MySQL. I solved it by routing the the call to execute through my own cursor like this:


Expand|Select|Wrap|Line Numbers
  1. class DBServer:
  2.     def __init__(self, master):
  3.         self.master = master
  4.  
  5.     def Login(self, servername, username, password, database=""):
  6.         try:
  7.             self.dbconnect = connect(host=servername, user=username, passwd=password, db=database)
  8.         except (DatabaseError, OperationalError):
  9.             self.dbconnect = None
  10.             self.master.write('Couldn\'t connect to the database named %s on host %s'
  11.                               %(repr(database), repr(servername)))
  12.             return
  13.         self.dbcursor = self.dbconnect.cursor()
  14.         self.Execute('SET autocommit=1')
  15.         return self.dbconnect
  16.  
  17.     def DBError(self):
  18.         """Remove the current message from the cursor
  19.            and display it."""
  20.         try:
  21.             (error, message) = self.dbcursor.messages.pop()
  22.         except AttributeError:
  23.             (error, message) = self.dbconnect.messages.pop()
  24.         self.master.write('%s #%d:  %s' %(str(error).split('.')[-1],
  25.                                           message[0], message[1]))
  26.  
  27.     def Execute(self, query):
  28.         try:
  29.             cursor = self.dbcursor
  30.             now = time()
  31.             cursor.execute(query)
  32.             nRows = cursor.rowcount
  33.             self.master.write(query)
  34.             self.master.write("%d rows affected: %.2f sec." %(nRows, time() - now))
  35.         except (DatabaseError, OperationalError):
  36.             self.DBError()
  37.             return
  38.         return cursor
  39.  
  40.     def DBExists(self, database):
  41.         """Return True if database exists"""
  42.         resultset = self.Execute("show databases").fetchall()
  43.         ## print resultset
  44.         return (database,) in resultset
  45.  
  46.     def close(self):
  47.         self.dbconnect.close()
I haven't seen the oracle module interface, but if it comforms the the python API, this should work for you, too. You are using python, right?
Nov 28 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.