2

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.

5
  • your code looks fine, can you add the structure of your table? is CatalogNbr string or number in dynamo table? Commented Mar 9, 2021 at 4:35
  • @BaluVyamajala Just added my table details. CatalogNbr is a string. Commented Mar 9, 2021 at 4:45
  • if CatalogNbr is string, is there a reason you are converting to number CatalogNbr = int(CatalogNbr) ? can you remove this line and try? Commented Mar 9, 2021 at 5:00
  • @BaluVyamajala Whoops! Must have missed that when I changed CatalogNbr to string. Thank you! Commented Mar 9, 2021 at 5:16
  • Great. glad it worked. Commented Mar 9, 2021 at 5:23

1 Answer 1

1

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.

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

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.