【问题标题】:AttributeError: 'list' object has no attribute 'values' python select queryAttributeError:“列表”对象没有属性“值”python选择查询
【发布时间】:2022-01-19 07:58:10
【问题描述】:

我有一个问题。我的查询返回我想要作为字典的结果。我使用 python 3.7 和 mysql。我在 ubuntu 18 喜欢:

[{'community_string': 'public'}, {'community_string': 'private'}]

我只想使用 public 和 private 作为字符串。为此,我编写了这样的代码:

query= "SELECT community_string FROM nat_snmp_string WHERE status ='enable'"
cursor = connection.cursor(dictionary=True)
cursor.execute(query)
com_str = cursor.fetchall()
com_str_string = com_str.values()
cmnyt_str = json.dumps(list(com_str))
print(cmnyt_str)  
cmd_line = cmnyt_str.replace(':','')
cmd_line = cmd_line.replace('"','')
cmd_line = cmd_line.replace(']','')
cmd_line = cmd_line.replace('[','')
   

但它给了我 AttributeError: 'list' object has no attribute 'values' 错误。 我该如何解决这个问题?

【问题讨论】:

  • cursor.fetchall() 似乎返回的是一个列表,而不是字典。
  • 我应该使用 cursor.fethone() 吗?我是新手,对不起。在 db 中,我可能有超过 2 个值,因为我使用 fetchall

标签: python mysql


【解决方案1】:

不清楚,你想做什么。 是否要获取“community_string”的值?

for r in cursor.fetchall():
   print(r['community_string'])

我不明白你对json和字符串替换操作做了什么......

【讨论】:

  • 是的,我想获取社区字符串的值。我试过但它给出了错误---> mysql.connector.errors.InterfaceError: No result set to fetch from
  • 它有效。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多