0

I'd like to get values except for NULL. Here's example.

from sqlalchemy import select
select([table]).execute().fetchone()
 -> ('1', 'some value', 'some value', None, None)

I don't want to get None. Is there any way to get values except for NULL?

Thanks, in advance.

2 Answers 2

1

It's returning you all columns by default. You should select only the columns you want to avoid the extra NULL/None values.

The docs have more info on that. Since you are passing the whole table you get all the columns.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. When I select all columns including NULL/None values, is there any way to automatically skip NULL/None values? I want exactly this function.
1

A fundamental feature of SQL is that a query returns the same number of columns for each row, so there's no way not to return a column based on some per-row condition on the SQL side. SQLAlchemy just follows this logic.

If you want to exclude None values from the data returned by SQLAlchemy, you can do it in Python:

def no_nulls(row):
    return [column for column in row if column is not None]

no_nulls(select([table]).execute().fetchone())
 -> ('1', 'some value', 'some value')

If a column is NULL in some rows but not in others, this approach may return different number of elements for different rows.

1 Comment

Thanks. I'll use function like this.

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.