2017-09-03 23 views
-1
source_table = raw_input("Enter the table name : ") 
PK = raw_input("Enter the primary key : ") 
PriKeyData = raw_input("Enter the data type for Primary key int-number, str-string :") 
with open('PriSortKeys.csv', 'rb') as csvfile: 
    csvreader = csv.reader(csvfile, delimiter='\t', quotechar='|') 
    for row in csvreader: 
     if PriKeyData == "int": 
       prikeyvalue = int(row[0]) 
      else: 
       prikeyvalue = str(row[0]) 
      logger.info("Checking for Key :" + str(prikeyvalue)) 
      ## Fetching data from table based on primarykey 
      sourcetable_data= source_table.query(KeyConditionExpression=Key(PK).eq(prikeyvalue)) 

我想把主鍵和它的值作爲用戶輸入並試圖查詢,但我得到以下錯誤:我越來越AttributeError:'str'對象在python中沒有屬性'查詢'

'str' object has no attribute 'query'

+1

'source_table = raw_input()'...那總是一個'str' ...你試圖運行query()對什麼對象? –

回答

0
source_table = raw_input("Enter the table name : ") 

將返回表的只是名稱,而不是表本身。你必須首先打開桌子,然後在桌子上打開query()

0
source_table = raw_input("Enter the table name : ") 
PK = raw_input("Enter the primary key : ") 
PriKeyData = raw_input("Enter the data type for Primary key int-number, str-string :") 
dynamodb = boto3.resource('dynamodb') 
s_table = dynamodb.Table(source_table.format(**locals())) 
with open('PriSortKeys.csv', 'rb') as csvfile: 
    csvreader = csv.reader(csvfile, delimiter='\t', quotechar='|') 
    for row in csvreader: 
     if PriKeyData == "int": 
       prikeyvalue = int(row[0]) 
      else: 
       prikeyvalue = str(row[0]) 
     logger.info("Checking for Key :" + str(prikeyvalue)) 
     ## Fetching data from table based on primarykey 
     s_table = source_table.query(KeyConditionExpression=Key(PK).eq(prikeyvalue))