python-3.xamazon-dynamodbboto3partiql

How to use f-Literal with PartiQL in AWS and boto3 and a Condition on the sort key


This is my current Code

table_name = 'TableName'
pk = "CID-73665"
Condition = "begins_with(sk,'CUS#')"

# get item 
stmt = f"SELECT * FROM {table_name} WHERE pk=? and {Condition}"
pmt =[{
    "S": pk
    },
    {
     "S": sk   
    }]

resp = dynamodb_client.execute_statement(
    Statement=stmt , Parameters= pmt
 )

pp.pprint(resp['Items'])

But i get this error message:

NameError: name 'sk' is not defined

Anyone has an idea what could be wrong?


Solution

  • Well, 'sk' is not defined. You try like this

    table_name = "TableName"
    pk = "CID-73665"
    sk = "CUS#"
    
    # get item
    stmt = f"SELECT * FROM {table_name} WHERE pk=? and begins_with('sk', ?)"
    pmt = [{"S": pk}, {"S": sk}]
    
    resp = dynamodb_client.execute_statement(Statement=stmt, Parameters=pmt)
    
    pp.pprint(resp["Items"])