2

I am reading from an Oracle database and would like to check if the field is column is of type cx_Oracle.DATETIME.

I have tried the following:

if columnTypes[columnIndex] == cx_Oracle.DATETIME:

and

if columnTypes[columnIndex] is cx_Oracle.DATETIME:

Neither work.

If I do:

print columnTypes[columnIndex]

it returns:

<type 'cx_Oracle.DATETIME'>

EDIT:

It worked by storing the type in a variable:

dbDateType = cx_Oracle.DATETIME

and

if columnTypes[columnIndex] == dbDateType

Cheetah
  • 13,785
  • 31
  • 106
  • 190

2 Answers2

2

The best way is probably to do:

isinstance(columnTypes[columnIndex], cx_Oracle.DATETIME)

If you want to check for exact type, type(obj) == myType woudl work, but using isinstance allows for sub-typing.

Vatine
  • 20,782
  • 4
  • 54
  • 70
  • I had already tried this, but I didn't put it in the question, because `columnTypes[columnIndex]` is not an instance, its a type. – Cheetah Jun 21 '13 at 12:25
  • I think you have the arguments reversed. It seems to me `columnTypes` is a list of actual *types*, and `cx_Oracle.DATETIME` would be the object that is an instance of that type. – kojiro Jun 21 '13 at 12:25
  • @kojiro is `cx_Oracle.DATETIME` not a type itself? – Cheetah Jun 21 '13 at 12:33
  • @Ben well, its `str` or `unicode` certainly wants you to think it is. It probably is, but that wasn't obvious from the start. – kojiro Jun 21 '13 at 12:38
  • @Ben `cx_Oracle.DATETIME` is an instance of a `cx_Oracle.DATETIME` object. Confusing, but that means it's a [singleton](http://en.wikipedia.org/wiki/Singleton_pattern). – 2rs2ts Jun 21 '13 at 15:30
2

Does the column columnTypes store the types themselves or representations of the types as for instance a string or type instance?

Try getting the output of both columnTypes[columnIndex] and type(columnTypes[columnIndex]). Hopefully one will give you <type 'cx_Oracle.DATETIME'>, which you can then compare to type(cx_Oracle.DATETIME).

If you get a super/subclass you will have to use isinstance in a similar fashion - this question might be of interest.

Community
  • 1
  • 1
KendallV
  • 396
  • 3
  • 15