Getting column names from a cursor using ODBC module?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dananrg@yahoo.com

    Getting column names from a cursor using ODBC module?

    Is there any way to retrieve column names from a cursor using the ODBC
    module? Or must I, in advance, create a dictionary of column position
    and column names for a particular table before I can access column
    values by column names? I'd prefer sticking with the ODBC module for
    now because it comes standard in Python.

    I'm using Python 2.4 at the moment.

    Thanks.
  • John Machin

    #2
    Re: Getting column names from a cursor using ODBC module?

    On Jun 21, 11:58 pm, dana...@yahoo.c om wrote:
    Is there any way to retrieve column names from a cursor using the ODBC
    module? Or must I, in advance, create a dictionary of column position
    and column names for a particular table before I can access column
    values by column names? I'd prefer sticking with the ODBC module for
    now because it comes standard in Python.
    >
    I'm using Python 2.4 at the moment.
    >
    Thanks.

    Comment

    • John Machin

      #3
      Re: Getting column names from a cursor using ODBC module?

      On Jun 21, 11:58 pm, dana...@yahoo.c om wrote:
      Is there any way to retrieve column names from a cursor using the ODBC
      module? Or must I, in advance, create a dictionary of column position
      and column names for a particular table before I can access column
      values by column names? I'd prefer sticking with the ODBC module for
      now because it comes standard in Python.
      >
      I'm using Python 2.4 at the moment.
      >
      Do you mean the odbc module? If so, it doesn't come standard in
      Python; it's part of the win32 package.

      I haven't used it for years -- my preference on Windows these days
      would be mxODBC if the client would pay the licence fee, otherwise
      pyodbc. Sorry I'm not answering your question ... perhaps you should
      be asking a different question :)

      Cheers,
      John

      Comment

      • John Machin

        #4
        Re: Getting column names from a cursor using ODBC module?

        On Jun 22, 12:19 am, John Machin <sjmac...@lexic on.netwrote:
        On Jun 21, 11:58 pm, dana...@yahoo.c om wrote:
        >
        Is there any way to retrieve column names from a cursor using the ODBC
        module? Or must I, in advance, create a dictionary of column position
        and column names for a particular table before I can access column
        values by column names? I'd prefer sticking with the ODBC module for
        now because it comes standard in Python.
        >
        I'm using Python 2.4 at the moment.
        >
        Do you mean the odbc module? If so, it doesn't come standard in
        Python; it's part of the win32 package.
        >
        I haven't used it for years -- my preference on Windows these days
        would be mxODBC if the client would pay the licence fee, otherwise
        pyodbc. Sorry I'm not answering your question ... perhaps you should
        be asking a different question :)
        >
        Cheers,
        John
        But to help you answer your question: if the module that you are using
        supports the 2.0 version of the database API (see http://www.python.org/dev/peps/pep-0249/),
        then it will support the cursor.descript ion attribute, which gives you
        not only the name but the type and 5 other bits of info about each
        column. If it doesn't, I'd suggest moving on.

        HTH,
        John

        Comment

        • Chris

          #5
          Re: Getting column names from a cursor using ODBC module?

          On Jun 21, 3:58 pm, dana...@yahoo.c om wrote:
          Is there any way to retrieve column names from a cursor using the ODBC
          module? Or must I, in advance, create a dictionary of column position
          and column names for a particular table before I can access column
          values by column names? I'd prefer sticking with the ODBC module for
          now because it comes standard in Python.
          >
          I'm using Python 2.4 at the moment.
          >
          Thanks.
          You should be able to do

          column_names = [d[0] for d in cursor.descript ion]

          Comment

          • dananrg@yahoo.com

            #6
            Re: Getting column names from a cursor using ODBC module?

            Thanks Chris and John. Chris, this worked perfectly with the ODBC
            module that ships with Python Win32:
            column_names = [d[0] for d in cursor.descript ion]
            John, I've never heard of pyodbc but I'll have to look into it.

            Thanks again.

            Dana

            Comment

            Working...