AWS DynamoDB Python – not returning attributes from table.scan()

I’m using Python to add data to a DynamoDB table named Courses and provide a command-interface for a user to search for a course description by entering the subject and catalog number.

The desired result:

Welcome to the Course Title Navigator

Enter Subject: SDEB
Enter Catalog Number: 452

Inaccurate search or No records found.  Please try again!

Enter Subject: SDEV
Enter Catalog Number: 450

SDEV 450 is Advanced Programming

Would you like to search for another title? Y or N
N

Thank you for using the Course Title Navigator

I am having issues with my program not returning table attributes after table.scan(). It seems to read the response as 0 and print the error message. I think there’s an issue with my table.scan, but I’m not sure what. I initially was using query, but I was having a ton of issues with it, and I read that scan is more suited for my application.

def check_class_info(Subject, CatalogNbr, Title, dynamodb=None): # checks if class info exists
    if not dynamodb:
        dynamodb = boto3.resource('dynamodb')
    
    while True:
        Subject = ""
        CatalogNbr = ""
        
        while Subject == "":
            Subject = input ("Enter Subject: ")
        CatalogNbr = ""
            
        while CatalogNbr == "":
            CatalogNbr = input("Enter Catalog Number: ")
        CatalogNbr = int(CatalogNbr)
                
        response = table.scan(FilterExpression=Attr("Subject").eq(Subject) & Attr("CatalogNbr").eq(CatalogNbr))

    # print(response)
        if response["Count"] == 0:
            print ("Inaccurate search or No records found.  Please try again!")
            return end_main()
        else:
            print(response["Items"][0]["title"])
            return end_main()

Here are the details of my table:

    aws dynamodb describe-table --table-name Courses
{
    "Table": {
        "TableArn": "arn:aws:dynamodb:us-east-1:399520938535:table/Courses", 
        "AttributeDefinitions": [
            {
                "AttributeName": "CourseID", 
                "AttributeType": "S"
            }, 
            {
                "AttributeName": "Subject", 
                "AttributeType": "S"
            }
        ], 
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0, 
            "WriteCapacityUnits": 50, 
            "ReadCapacityUnits": 50
        }, 
        "TableSizeBytes": 753, 
        "TableName": "Courses", 
        "TableStatus": "ACTIVE", 
        "TableId": "d62be64f-949d-454c-b716-93ff18968d58", 
        "KeySchema": [
            {
                "KeyType": "HASH", 
                "AttributeName": "CourseID"
            }, 
            {

I’m still learning DynamoDB, so I’m struggling to solve the error. I really appreciate any help you can provide.

Answer

Scan operation can be run in two ways

response = table.scan(FilterExpression=Attr("Subject").eq(Subject) & Attr("CatalogNbr").eq(CatalogNbr))

OR

response = table.scan(
        FilterExpression= 'Subject=:subject AND CatalogNbr=:catalogNbr',
        ExpressionAttributeValues= {
                ':subject': Subject ,
                ':catalogNbr': CatalogNbr,
        } )

In both types of syntax we must pass the right type. From the comments, in this case CatalogNbr which is a string in database must be passed as string. Hence removing CatalogNbr = int(CatalogNbr) line worked.

Leave a Reply

Your email address will not be published. Required fields are marked *