Somewhere I'm being an idiot, but I can't find where.
I'm running a Python script using a PostgreSQL database through ODBC. I am trying to extract the meaningful piece from a database exception message. Here is the raw message, with line breaks added for readability:
(-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for ODBC Drivers',
'ERROR: Charge not in a correct status to delete;\nError while executing the query',
None, 0, -2147467259), None)
Note that there are two sets of parentheses in this string. First, I find the locations of the outer ones and slice them off. This gives the expected result:
-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for ODBC Drivers',
'ERROR: Charge not in a correct status to delete;\nError while executing the query',
None, 0, -2147467259), None
Then, using as far as I can tell identical code, I strip off the other set of parentheses and everything outside them. This gives this result:
(0, 'Microsoft OLE DB Provider for ODBC Drivers',
'ERROR: Charge not in a correct status to delete;\nError while executing the query',
None, 0, -214746725
The open parenthesis is still here, even though I am using the result of the find() method the same way, adding one to the open parenthesis location as the start of the slice, both times.
Here is the code:
print (errorString)
openParenLocation = errorString.find('(')
closeParenLocation = errorString.rfind(')')
strippedString = errorString[openParenLocation + 1:closeParenLocation]
openParenLocation = strippedString.find('(')
closeParenLocation = strippedString.rfind(')')
dbErrorString = errorString[openParenLocation + 1:closeParenLocation]
print (strippedString)
print ("{}, {}".format(openParenLocation, closeParenLocation))
print (dbErrorString)
And here is the raw output, with no added line breaks:
(-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for ODBC Drivers', 'ERROR: Charge not in a correct status to delete;\nError while executing the query', None, 0, -2147467259), None)
-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for ODBC Drivers', 'ERROR: Charge not in a correct status to delete;\nError while executing the query', None, 0, -2147467259), None
36, 191
(0, 'Microsoft OLE DB Provider for ODBC Drivers', 'ERROR: Charge not in a correct status to delete;\nError while executing the query', None, 0, -214746725
Test code using a much smaller string works as expected:
testString = "(abc(def)ghij)"
openParenLocation = testString.find('(')
closeParenLocation = testString.rfind(')')
strippedTestString = testString[openParenLocation + 1:closeParenLocation]
openParenLocation = strippedTestString.find('(')
closeParenLocation = strippedTestString.rfind(')')
finalTestString = strippedTestString[openParenLocation + 1:closeParenLocation]
Thank you very much.
errorString[openParenLocation + 1:closeParenLocation]??? what are you trying to do by usingopenParenLocation + 1:closeParenLocation?errorStringoriginally? It looks a lot like a python tuple'srepr. Perhaps the data is available as a tuple, which would make handling it a no brainer. To your question, you're forming the indices fordbErrorStringfronstrippedString, but then useerrorStringin the actual slicing instead ofstrippedString.None, certainly not SQL. The contents of that string at least are a valid tuple... This reeks of an XY.