1. <i id='FU1EF'><tr id='FU1EF'><dt id='FU1EF'><q id='FU1EF'><span id='FU1EF'><b id='FU1EF'><form id='FU1EF'><ins id='FU1EF'></ins><ul id='FU1EF'></ul><sub id='FU1EF'></sub></form><legend id='FU1EF'></legend><bdo id='FU1EF'><pre id='FU1EF'><center id='FU1EF'></center></pre></bdo></b><th id='FU1EF'></th></span></q></dt></tr></i><div id='FU1EF'><tfoot id='FU1EF'></tfoot><dl id='FU1EF'><fieldset id='FU1EF'></fieldset></dl></div>
    <tfoot id='FU1EF'></tfoot>

    1. <legend id='FU1EF'><style id='FU1EF'><dir id='FU1EF'><q id='FU1EF'></q></dir></style></legend>
        <bdo id='FU1EF'></bdo><ul id='FU1EF'></ul>

    2. <small id='FU1EF'></small><noframes id='FU1EF'>

      如何在 Python 中使用列名检索 SQL 结果列值?

      时间:2023-05-24
      <tfoot id='G4oS1'></tfoot>

      <legend id='G4oS1'><style id='G4oS1'><dir id='G4oS1'><q id='G4oS1'></q></dir></style></legend>

    3. <small id='G4oS1'></small><noframes id='G4oS1'>

            <bdo id='G4oS1'></bdo><ul id='G4oS1'></ul>

                <tbody id='G4oS1'></tbody>
                <i id='G4oS1'><tr id='G4oS1'><dt id='G4oS1'><q id='G4oS1'><span id='G4oS1'><b id='G4oS1'><form id='G4oS1'><ins id='G4oS1'></ins><ul id='G4oS1'></ul><sub id='G4oS1'></sub></form><legend id='G4oS1'></legend><bdo id='G4oS1'><pre id='G4oS1'><center id='G4oS1'></center></pre></bdo></b><th id='G4oS1'></th></span></q></dt></tr></i><div id='G4oS1'><tfoot id='G4oS1'></tfoot><dl id='G4oS1'><fieldset id='G4oS1'></fieldset></dl></div>
                本文介绍了如何在 Python 中使用列名检索 SQL 结果列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                有没有办法在 Python 中使用列名而不是列索引来检索 SQL 结果列值?我在 mySQL 中使用 Python 3.我正在寻找的语法非常类似于 Java 结构:

                Is there a way to retrieve SQL result column value using column name instead of column index in Python? I'm using Python 3 with mySQL. The syntax I'm looking for is pretty much like the Java construct:

                Object id = rs.get("CUSTOMER_ID"); 
                

                我有一个包含相当多列的表,不断为我需要访问的每一列计算索引真的很痛苦.此外,索引使我的代码难以阅读.

                I've a table with quite a number of columns and it is a real pain to constantly work out the index for each column I need to access. Furthermore the index is making my code hard to read.

                谢谢!

                推荐答案

                MySQLdb 模块有一个 DictCursor:

                像这样使用它(取自使用 Python DB-API 编写 MySQL 脚本):

                Use it like this (taken from Writing MySQL Scripts with Python DB-API):

                cursor = conn.cursor(MySQLdb.cursors.DictCursor)
                cursor.execute("SELECT name, category FROM animal")
                result_set = cursor.fetchall()
                for row in result_set:
                    print "%s, %s" % (row["name"], row["category"])
                

                根据 user1305650 这也适用于 pymysql.

                edit: According to user1305650 this works for pymysql as well.

                这篇关于如何在 Python 中使用列名检索 SQL 结果列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用同一表中另一行的数据更新行 下一篇:将现有表格中每个单词的首字母大写

                相关文章

                    <legend id='8gr0n'><style id='8gr0n'><dir id='8gr0n'><q id='8gr0n'></q></dir></style></legend>
                    • <bdo id='8gr0n'></bdo><ul id='8gr0n'></ul>

                    <small id='8gr0n'></small><noframes id='8gr0n'>

                  1. <i id='8gr0n'><tr id='8gr0n'><dt id='8gr0n'><q id='8gr0n'><span id='8gr0n'><b id='8gr0n'><form id='8gr0n'><ins id='8gr0n'></ins><ul id='8gr0n'></ul><sub id='8gr0n'></sub></form><legend id='8gr0n'></legend><bdo id='8gr0n'><pre id='8gr0n'><center id='8gr0n'></center></pre></bdo></b><th id='8gr0n'></th></span></q></dt></tr></i><div id='8gr0n'><tfoot id='8gr0n'></tfoot><dl id='8gr0n'><fieldset id='8gr0n'></fieldset></dl></div>

                    <tfoot id='8gr0n'></tfoot>