![](/img/trans.png)
[英]Code breakes while reading resultset returned by DyanmoDb's table.scan() in python
[英]AWS DynamoDB Python - not returning attributes from table.scan()
我正在使用 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,所以我正在努力解決這個錯誤。 我非常感謝您能提供的任何幫助。
掃描操作可以以兩種方式運行
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)
行有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.