AWS DynamoDB Python - 不从 table.scan() 返回属性
AWS DynamoDB Python - not returning attributes from table.scan()
我正在使用 Python 将数据添加到名为 Courses 的 DynamoDB table 并为用户提供一个命令界面,以便用户通过输入主题和目录号来搜索课程描述。
想要的结果:
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() 之后没有返回 table 属性时遇到问题。它似乎将响应读取为 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()
以下是我的 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"
},
{
我还在学习DynamoDB,所以我正在努力解决错误。非常感谢您提供的任何帮助。
扫描操作可以运行两种方式
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,
} )
在这两种语法中,我们都必须传递正确的类型。根据注释,在这种情况下,数据库中的字符串 CatalogNbr 必须作为字符串传递。因此删除 CatalogNbr = int(CatalogNbr)
行有效。
我正在使用 Python 将数据添加到名为 Courses 的 DynamoDB table 并为用户提供一个命令界面,以便用户通过输入主题和目录号来搜索课程描述。
想要的结果:
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() 之后没有返回 table 属性时遇到问题。它似乎将响应读取为 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()
以下是我的 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"
},
{
我还在学习DynamoDB,所以我正在努力解决错误。非常感谢您提供的任何帮助。
扫描操作可以运行两种方式
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,
} )
在这两种语法中,我们都必须传递正确的类型。根据注释,在这种情况下,数据库中的字符串 CatalogNbr 必须作为字符串传递。因此删除 CatalogNbr = int(CatalogNbr)
行有效。