Python - EOF/BOF exception when using GetRows() function (from MDX query) - python

I wrote this code to retrieve data from a cube:
rs = win32com.client.Dispatch('ADODB.RecordSet')
rs.__init__
conn = win32com.client.Dispatch('ADODB.Connection')
conn.__init__
conn.CommandTimeout=3000
conn.Open(connString)
#Retrieving data
try:
rs.Open(mdxQuery, conn, 1, 3)
except Exception as e:
print("An error occured while trying to retrieve data:")
print(str(e))
sys.exit(1)
try:
t=rs.GetRows()
except Exception as ex:
print(str(ex))
it seems the mdx query is ok, and retrieve some result (I can see it from debug mode)
but when I try to assign the result of GetRows() function to a variable, it fails, with the following message:
ADODB.Field : Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.
It seems to me GetRows() function is not able to handle NULL values. Is there a way to tell that method to skip NULL values or just fill it with blanks, and avoid this error?
Thanks

Related

continue to create tables in for loop after exception is raised

I have a list of employees and I want to create tables for them. I want to continue creating tables even if one of the table creations fail. I am using try/except block but when a table creation fails, it fails totally. I am trying below code. Appreciate your help!
employees=[1,2,3,4]
for p in employees:
create_table = f"""create table dim_{p}"""
try:
conn.execute(create_table)
print(f"success for {p}")
except Exception as e:
raise Exception(f"Failed to create table for {p} with error : {{e}}")
I think you can save the exception object for later without raising it.
employees=[1,2,3,4]
exceptions = []
for p in employees:
create_table = f"""create table dim_{p}"""
try:
conn.execute(create_table)
print(f"success for {p}")
except Exception as e:
exceptions.append((p, e)) # save info as a tuple
if exceptions: # after your loop over employees is complete
# handle your errors how you like
fail_msg = '\n'.join([f"{exception[0]} ; {exception[1]}" for exception in exceptions])
raise Exception(f"Failed to create table for the following employees:\n{fail_msg}")
When I run this (since conn is not defined via any imports) I get:
Exception: Failed to create table for the following employees:
1 ; name 'conn' is not defined
2 ; name 'conn' is not defined
3 ; name 'conn' is not defined
4 ; name 'conn' is not defined
I am using try/except block but when a table creation fails, it fails totally.
this is because you have used raise Exception(f"Failed to create table for {p} with error : {{e}}")
raising an exception, leading to the program interrupting. To avoid that, simply don't use raise.
For better understanding, look at the example below:
employees=["1","2",[1,2],"4"]
for i in employees :
try :
print(int(i))
except :
print("error on" , i)
This will output :
1
2
error on [1, 2]
4

why do Syntax error (python) doesn't work?

I want to print a message in case the user made a mistake while writing the code but it doesnt work I also tried to add NameError exception, it only works if I raise an exception.Thank you for helping.
`
def cncours(nvcours,num_cours):
try :
sql="Update cours set nomC=%s where num_cours=%s"
result=cursor.fetchone()
cursor.execute(sql,(nvcours,num_cours))
print("Operation Done.")
except TypeError:
print("Plz put the name between quotes")
`
Each DB implementation (MySQL, sqlite,...) may raise their particular exceptions. So instead of catching a general exceptions, you may catch errors depending on the specific DB, then on the particular type (e.g. SyntaxError). I suggest to provoque a syntax error on your SQL statement then see what type is (e.g. errorcode or exception type) then catch it.
For instance, the MySQL connector raises error numbers:
import mysql.connector as cn
try:
#...
except cn.Error as err:
print("Something went wrong: {}".format(err))
if err.errno == errorcode.ER_BAD_TABLE_ERROR: #
Here are some MySQL Error Code Ranges
If you are using MySQLdb:
import MySQLdb
try:
#...
cursor.execute(sql)
res = cursor.fetchone()
# ...
except MySQLdb.Error, e:
print "MySQLdb.Error: {}".format(e.args)
Depending on your schema (column types) and the type from the input variable, you may use:
sql="Update cours set nomC='%s' where num_cours=%s" # Added quotes on the first replacement
Besides what you are asking, I think that the command order is inverted.
sql="Update cours set nomC=%s where num_cours=%s"
cursor.execute(sql,(nvcours,num_cours)) # First
result=cursor.fetchone() # Second
print("Operation Done.")
https://www.tutorialspoint.com/python/python_database_access.htm
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()

how to use try and except in python?

I am trying to get the user_id from the sqlite3 database in python and store in in an array. This is what I have tried:
LIST_ARRAY=[]
def start(bot, update):
value = update.message.from_user.id
print("VALUE first: ", value)
try:
for row in connection.execute("SELECT *from userdetails WHERE user_id=?", (value,)):
print(row)
user_id, full_name, address, phone_number, email = row
data = [user_id]
LIST_ARRAY[0] = data[0]
except:
print(LIST_OF_ADMINS)
It doesn't get the value please help me to sort this out.
My data variable doesn't get added in the list array
i cant help you with the database issue but i can answer the question of "How to use try and except in python"
Try and Except are used in python to handle errors when they occur in your code as shown below
x=[0,'It Worked',2]
try:
print(x[4])
except IndexError:
print("Oh No there was an error")
try:
print(x[1])
except IndexError:
print("Oh No there was an error")
so in the example i made above i created an array with 2 ints and a string... I then "TRY" to print the contents in x[4] which do not exist which results in a IndexError... because i told the program to print(x[4]) and the error that i prepared for occurred the "EXCEPT" statement is executed and the code beneath it is also executed
the output of the code is:
'Oh No there was an error'
'It Worked'

Why fetchall always results in `None`?

Here are data in MySQL databaseļ¼š
python3 tableļ¼šusers_info
But when i use fetchall to get all data in tables it always return none!!
I really really dont konw how to fix it,can someone has met this problem?
the followings are the code files.
encapsulation.py
import MySQLdb
class mysql_encapsulation(object):
def __init__(self,host,port,user,passwd,db,charset):
self.host = host
self.port = port
self.user = user
self.passwd = passwd
self.db = db
self.charset = charset
def open(self):
self.conn = MySQLdb.connect(host=self.host,port=self.port,user=self.user,passwd=self.passwd,db=self.db,charset=self.charset)
self.cursor = self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def operate(self,sql,params):
try:
self.open()
self.cursor.execute(sql,params)
self.conn.commit()
print(' operate ok')
self.close()
except Exception,e:
print(e.message)
self.conn.rollback()
def get_result_set(self,sql,params=[]):
result=None
try:
self.open()
self.cursor.execute(sql,params)
result = self.cursor.fetchall()
self.close()
except Exception,e:
print('error!')
print(e.message)
return result
use.py(problem in this file)
#coding=utf-8
from encapsulation import *
mysql = mysql_encapsulation(port=3306,host='localhost',user='root',passwd='mysql',
db='python3',charset='utf8')
sql='select id,name from users_info where id=3'
result=mysql.get_result_set(sql)
print (result)
The problem happens in this method;
def get_result_set(self,sql,params=[]):
result=None
try:
self.open()
self.cursor.execute(sql,params)
result = self.cursor.fetchall()
self.close()
except Exception,e:
print('error!')
print(e.message)
return result
One of the first 3 lines in the try block (self.open / self.cursor.exec / result = self.cursor.fetch) generate an error, which you are then catching in the except block and (you can see that it prints "error!"). That's why the result always stays in it's default None value. Remove the except block and it will tell you what kind of error occured.
You should almost never catch the bare Exception but instead catch specific kinds of exception and handle each of them correctly, this problem is a prefect example why.
The error in question probably happens because in your SQL query you are selecting id and name, when the columns in your table are actually id and user_name. So your SQL query should be like this;
sql = 'select id, user_name from users_info where id = 3'
Did you check your sql statement? Based on your table your columns are "id", "user_name" and "passwd", but in your sql you are searching for "id" and "name", and "name" isn't a column so that will throw an error. Change your sql to "sql='select id,user_name from users_info where id=3'"
As ruohola already explained in his answer, your exception handler is hiding all the important informations about what really went wrong - FWIW, there's the "Error !" string printed in your screen capture just above the None. The point is: your try/except block is not only useless, it's actually harmful - it prevents some other code up the call stack to even be aware that there was a problem (and eventually solve it), it also prevents you from knowing what went wrong. As a general rule, only catch exception the exact exception that you expect AND can effectively handle at this point in the code - if you can't fix the error at this point, let the exception propagate and let the calling code deal with it. Also, you want to have as few code as possible in your try clause (the only exception being the application's top-level exception handler of course).
This being said, there is indeed a very valid reason for wanting to be warned of exception, which is to make sure you free resources (and eventually rollback a transaction), but then you want 1/ to use a finally clause for resources cleanup (a finally block is always executed, whatever happens) and 2/ for the rollback part, use an except clause but re-raise the exception, ie:
# leave this one out of the try block -
# there's nothing you can do here about a connection error
# so let the calling code deal with it
self.connect()
try:
self.cursor.execute(sql, params)
except Exception as e:
self.connection.rollback()
finally:
# this will ALWAYS be executed (unless `self.connect()` raised
# but then you don't even want to close the connection <g>
self.close()
wrt/ the real cause of your problem, Kurose's answer is certainly the right one.
Also, there are a quite a few other things that are debatable with your "mysql_encapsulation" class (the naming to start with but let ignore this ATM), the main one being to open and close the connection for each query. Opening a database connection has a rather high cost, so you want to keep it opened as long as possible (and eventually reconnect if you get a "2006 mysql has gone away" error).

oracle error code handling cx_oracle

I'm trying to accessing Oracle from Python using cx_ORacle.
I have a select statement that returns no rows ie; NO_DATA_FOUND..and this is how Im handling the error condition.
When I execute this piece of code, the error NO_DATA_FOUND is not catched by the cx_oracle.error or cx_oracle.Databaseerror or cx_oracle.Warning..
How do I handle the NO_DATA_FOUND condition?
code.py
def DetermineNames(self):
sql = """select NAME from EMP_TAB where fd_fle_id = %s"""%(self.fileid)
try:
self.cursor.execute(sql)
result = self.cursor.fetchall()
for row in result:
print('row',row)
except cx_Oracle.Error as e:
print("Error:Unable to determine the RAW_OBJ_NAME: Object Name:%s, Function Name:%s Error:%s")%(self.__class__.__name__,sys._getframe().f_code.co_name,
str(e).strip())
except cx_Oracle.DatabaseError as e:
print("Error:Unable to determine the RAW_OBJ_NAME: Object Name:%s, Function Name:%s Error:%s")%(self.__class__.__name__,sys._getframe().f_code.co_name,
str(e).strip())
except cx_Oracle.Warning as e:
print("Error:Unable to determine the RAW_OBJ_NAME: Object Name:%s, Function Name:%s Error:%s")%(self.__class__.__name__,sys._getframe().f_code.co_name,
str(e).strip())
return self.rawname
This isn't an error condition, which is probably why none of your exceptions are being triggered. Try something like this instead:
if not self.cursor.rowcount:
print 'No rows returned'
May be use cx_Oracle._Error.code and if the value is 1403 then its a NO DATA FOUND.
....
except cx_Oracle.DatabaseError as e:
error, = e.args
if error.code == 1403:
print "No rows returned. Error Code:", error.code
....

Categories

Resources