0

I have a clob datapoint that I must use .read() to add it to a list, however, sometimes this column is null, so I need a check first before using the .read() property.

I've isolated the portion that is relevant. If I just print data, the null fields print as none. Is not null seems to be the wrong code, but I'm not sure what to use.

for currentrow in data:
    if currentrow[8] is not null:
        Product = currentrow[8].read()
    else:
        Product = currentrow[8]
    data = tuple([currentrow[0], currentrow[1], currentrow[2], currentrow[3], currentrow[4], currentrow[5], currentrow[6], currentrow[7], Product])
print data

2 Answers 2

4

From the docs:

The sole value of types.NoneType. None is frequently used to represent the absence of a value, as when default arguments are not passed to a function.

So you may try this:

for currentrow in data:
    if currentrow[8] is not None:   <-- Change this from null to None
        Product = currentrow[8].read()
    else:
        Product = currentrow[8]
    data = tuple([currentrow[0], currentrow[1], currentrow[2], currentrow[3], currentrow[4], currentrow[5], currentrow[6], currentrow[7], Product])
print data
Sign up to request clarification or add additional context in comments.

Comments

3

Python uses the None singleton value as a null; NULLs from the database are translated to that object:

if currentrow[8] is not None:

You could collapse that line into just two:

for currentrow in data:
    product = currentrow[8] and currentrow[8].read()
    data = currentrow[:8] + (product,)

as Python's and operator short-circuits and None is false in a boolean context. Unless you set a row factory, cx_Oracle cursors produce tuples for each row, which you can slice to select just the first 8 elements, then append the 9th to create a new tuple from the two.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.