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

    1. <legend id='nQdxv'><style id='nQdxv'><dir id='nQdxv'><q id='nQdxv'></q></dir></style></legend>

        <small id='nQdxv'></small><noframes id='nQdxv'>

      1. MySQL:从查询中获取列名或别名

        时间:2023-06-01
      2. <small id='M58Za'></small><noframes id='M58Za'>

        <tfoot id='M58Za'></tfoot>

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

              <tbody id='M58Za'></tbody>
              <legend id='M58Za'><style id='M58Za'><dir id='M58Za'><q id='M58Za'></q></dir></style></legend>
                1. 本文介绍了MySQL:从查询中获取列名或别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我不是要求 SHOW COLUMNS 命令.

                  我想创建一个与 heidisql 类似的应用程序,您可以在其中指定 SQL 查询,并在执行时返回一个结果集,其中的行和列代表您的查询结果.结果集中的列名应与您在 SQL 查询中定义的所选列相匹配.

                  I want to create an application that works similarly to heidisql, where you can specify an SQL query and when executed, returns a result set with rows and columns representing your query result. The column names in the result set should match your selected columns as defined in your SQL query.

                  在我的 Python 程序(使用 MySQLdb)中,我的查询仅返回行和列结果,而不返回列名称.在以下示例中,列名称将是 exttotalsizefilecount.SQL 最终会在程序之外.

                  In my Python program (using MySQLdb) my query returns only the row and column results, but not the column names. In the following example the column names would be ext, totalsize, and filecount. The SQL would eventually be external from the program.

                  我能想到的唯一方法是编写我自己的 SQL 解析器逻辑来提取选定的列名.

                  The only way I can figure to make this work, is to write my own SQL parser logic to extract the selected column names.

                  是否有一种简单的方法可以获取所提供 SQL 的列名?接下来我需要知道查询返回多少列?

                  Is there an easy way to get the column names for the provided SQL? Next I'll need to know how many columns does the query return?

                  # Python
                  
                  import MySQLdb
                  
                  #===================================================================
                  # connect to mysql
                  #===================================================================
                  
                  try:
                      db = MySQLdb.connect(host="myhost", user="myuser", passwd="mypass",db="mydb")
                  except MySQLdb.Error, e:
                      print "Error %d: %s" % (e.args[0], e.args[1])
                      sys.exit (1)
                  
                  #===================================================================
                  # query select from table
                  #===================================================================
                  
                  cursor = db.cursor ()   
                  
                  cursor.execute ("""\
                       select ext,
                          sum(size) as totalsize,
                          count(*) as filecount
                       from fileindex
                      group by ext
                      order by totalsize desc;
                  """)
                  
                  while (1):
                      row = cursor.fetchone ()
                      if row == None:
                          break
                      print "%s %s %s\n" % (row[0], row[1], row[2])
                  
                  cursor.close()
                  db.close()      
                  

                  推荐答案

                  cursor.description 将为您提供一个元组元组,其中每个 [0] 是列标题.

                  cursor.description will give you a tuple of tuples where [0] for each is the column header.

                  num_fields = len(cursor.description)
                  field_names = [i[0] for i in cursor.description]
                  

                  这篇关于MySQL:从查询中获取列名或别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何移动 MySQL 表中的列? 下一篇:MySQL选择其中列不为空的地方

                  相关文章

                    <small id='As2He'></small><noframes id='As2He'>

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

                    1. <tfoot id='As2He'></tfoot>