【问题标题】:AWS DynamoDB Python - not returning attributes from table.scan()AWS DynamoDB Python - 不从 table.scan() 返回属性
【发布时间】:2021-03-09 04:00:46
【问题描述】:

我正在使用 Python 将数据添加到名为 Courses 的 DynamoDB 表中,并为用户提供一个命令界面,以通过输入主题和目录号来搜索课程描述。

想要的结果:

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

我的程序在 table.scan() 之后没有返回表属性时遇到问题。似乎将响应读取为 0 并打印错误消息。我认为我的 table.scan 存在问题,但我不确定是什么问题。我最初使用的是查询,但我遇到了很多问题,我读到扫描更适合我的应用程序。

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()

这是我的桌子的详细信息:

    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"
            }, 
            {

我还在学习 DynamoDB,所以我正在努力解决错误。非常感谢您提供的任何帮助。

【问题讨论】:

  • 您的代码看起来不错,可以添加表格的结构吗?发电机表中的 CatalogNbr 是字符串还是数字?
  • @BaluVyamajala 刚刚添加了我的餐桌详情。 CatalogNbr 是一个字符串。
  • 如果 CatalogNbr 是字符串,您是否有理由将其转换为数字 CatalogNbr = int(CatalogNbr) ?你能把这条线去掉试试吗?
  • @BaluVyamajala 哎呀!当我将 CatalogNbr 更改为字符串时,一定错过了这一点。谢谢!
  • 太棒了。很高兴它成功了。

标签: python amazon-dynamodb boto3 aws-cloud9


【解决方案1】:

扫描操作可以通过两种方式运行

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

或

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

在这两种语法中,我们都必须传递正确的类型。在 cmets 中,在这种情况下 CatalogNbr 是数据库中的字符串,必须作为字符串传递。因此删除CatalogNbr = int(CatalogNbr) 行有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多