0
votes

I want to use a filter on query_entities for Azure table storage.

I have tried to use filter like this : Table = table_service.query_entities('MyTableName', filter = "RowKey eq 20")

#coding:utf-8
import os
import json
from azure import *
from azure.storage import *
from azure.storage.table import TableService, Entity

table_service = TableService(account_name='MyAccountName', 
sas_token='MySASToken')
Table  = table_service.query_entities('MyTableName', filter = "Country eq 
USA")
print(Table.items)

I got an exception:

azure.common.AzureHttpError: Bad Request {"odata.error":{"code":"InvalidInput","message":{"lang":"en-US","value":"A binary operator with incompatible types was detected. Found operand types 'Edm.String' and 'Edm.Int32' for operator kind 'Equal'.\nRequestId:ef3858e7-5002-00d0-617f-0d374a000000\nTime:2019-05-18T13:45:23.6288160Z"}}}

And I have tried changing to this :

Table  = table_service.query_entities('MyTableName', filter = "Country eq 
USA")

But I get SyntaxError: invalid syntax

3

3 Answers

0
votes

Try this for filter:

Country eq 'USA'

Basically your attribute's data type is String hence the value needs to be in single quotes.

Same thing applies for your RowKey query as well:

RowKey eq '20'

You can find more examples here: https://docs.microsoft.com/en-us/rest/api/storageservices/querying-tables-and-entities#sample-query-expressions.

0
votes

You got the first exception because of the equal comparison: "Country eq USA".

Check your table description, in exception message it's said you are doing "Found operand types 'Edm.String' and 'Edm.Int32' for operator kind 'Equal'". Country is likely be Edm.Int32, so make sure you are comparing the same type.

And the second exception though you didn't post full traceback I suspect you deleted the last line's parenthesis.

0
votes

I have tried this and it's work fine :

Table  = table_service.query_entities('MyTableName', filter = "Country eq 
'USA'")