【问题标题】:Get sql query value in Django在Django中获取sql查询值
【发布时间】:2021-10-09 17:38:39
【问题描述】:

我怎样才能只获得以下查询的值。我只想获取 name 的值,而不是 dict,因为在 laravel 中您可以使用我可以使用的 django 中的 -> get ()。

with connection.cursor() as cursor:
    cursor.execute("SELECT name FROM person WHERE id=2")
    fila=cursor.fetchall()

谢谢。

【问题讨论】:

    标签: python html sql django django-models


    【解决方案1】:

    您可以简单地使用索引。索引将返回分配给特定键的值。所以你可以先

    print(cursor.fetchall())
    

    然后您可以确定需要返回的索引。然后您可以根据需要使用 for 循环或直接索引

    fila = cursor.fetchall()[indexvalue]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多