pyodbc: help interpreting help

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

pyodbc: help interpreting help

Goldsmith, David

Hi!  Probably ‘cause I’m new to DBs, I’m having trouble understanding pyodbc’s help.  For example, what kind of Python object is a “results set”?  In particular, the syntax portion of the docstring for the tables method of a cursor object states that it returns self, but the text portion of the docstring says it “creates a results set of tables defined in the data source.”  It goes on to say that “each row fetched has the following columns: …2) table_name: The table name…”  It is simply a Python list of these that I want, but the API for actually returning such is far from clear: how does one access the collection of “fetched rows” (what kind of Python object is the collection and what is its name in the local dictionary) and how does one “slice out” one or more columns from it?  Please help.  Thanks!

 

--------------------------------------
David Goldsmith
Washington State Department of Ecology
Environmental Assessment Program
Modeling and Information Support Unit
300 Desmond Drive  | P.O. Box 47600
Lacey, WA  98503   | Olympia, WA  98504-7600
Tel:     (360) 407-6194
Fax:     (360) 407-6884

Email:   [hidden email]
Station: C2D-59

Web:     http://www.ecy.wa.gov/programs/eap/

 


_______________________________________________
DB-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig
Reply | Threaded
Open this post in threaded view
|

Re: pyodbc: help interpreting help

Carl Karsten
On Wed, Oct 6, 2010 at 1:40 PM, Goldsmith, David <[hidden email]> wrote:

> Hi!  Probably ‘cause I’m new to DBs, I’m having trouble understanding
> pyodbc’s help.  For example, what kind of Python object is a “results set”?
> In particular, the syntax portion of the docstring for the tables method of
> a cursor object states that it returns self, but the text portion of the
> docstring says it “creates a results set of tables defined in the data
> source.”  It goes on to say that “each row fetched has the following
> columns: …2) table_name: The table name…”  It is simply a Python list of
> these that I want, but the API for actually returning such is far from
> clear: how does one access the collection of “fetched rows” (what kind of
> Python object is the collection and what is its name in the local
> dictionary) and how does one “slice out” one or more columns from it?
> Please help.  Thanks!
>

The odbc module that comes with python isn't very good.  There are a
handful of additional ones you can pick from.  Here is a rundown I
made afew years ago:

http://wiki.python.org/moin/ODBC

--
Carl K
_______________________________________________
DB-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig
Reply | Threaded
Open this post in threaded view
|

Re: pyodbc: help interpreting help

ccurvey
In reply to this post by Goldsmith, David
Maybe a quick example would help.

# connection strings vary, see www.connectionstrings.com for an example for your database
connection = pyodbc.connect("your-connection-string-here")

# grab a cursor from the connection (you can have multiple cursors from a connection)
cursor = connection.cursor()

# assumes you have a table called 'addreses' with three columns
cursor.execute("select name, city, state from addresses")

# fetchall() returns a list (tuple?) of tuples.  So it would look like
# ( ( "Bill", "Redmond", "WA),
#   ( "Steve", "Cupertino", "CA"),
#   ("Barack", "Washington", "DC")
#)

# gotta handle all the fetched stuff
for (name, city, state) in cursor.fetchall():
    # just print the name and state
    print name, state

Does that help?

On Wed, Oct 6, 2010 at 2:40 PM, Goldsmith, David <[hidden email]> wrote:

Hi!  Probably ‘cause I’m new to DBs, I’m having trouble understanding pyodbc’s help.  For example, what kind of Python object is a “results set”?  In particular, the syntax portion of the docstring for the tables method of a cursor object states that it returns self, but the text portion of the docstring says it “creates a results set of tables defined in the data source.”  It goes on to say that “each row fetched has the following columns: …2) table_name: The table name…”  It is simply a Python list of these that I want, but the API for actually returning such is far from clear: how does one access the collection of “fetched rows” (what kind of Python object is the collection and what is its name in the local dictionary) and how does one “slice out” one or more columns from it?  Please help.  Thanks!

 

--------------------------------------
David Goldsmith
Washington State Department of Ecology
Environmental Assessment Program
Modeling and Information Support Unit
300 Desmond Drive  | P.O. Box 47600
Lacey, WA  98503   | Olympia, WA  98504-7600
Tel:     (360) 407-6194
Fax:     (360) 407-6884

Email:   [hidden email]
Station: C2D-59

Web:     http://www.ecy.wa.gov/programs/eap/

 


_______________________________________________
DB-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig




--
Ignoring that little voice in my head since 1966!

_______________________________________________
DB-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig
Reply | Threaded
Open this post in threaded view
|

Re: pyodbc: help interpreting help

M.-A. Lemburg
In reply to this post by Goldsmith, David
Goldsmith, David wrote:

> Hi!  Probably 'cause I'm new to DBs, I'm having trouble understanding
> pyodbc's help.  For example, what kind of Python object is a "results
> set"?  In particular, the syntax portion of the docstring for the tables
> method of a cursor object states that it returns self, but the text
> portion of the docstring says it "creates a results set of tables
> defined in the data source."  It goes on to say that "each row fetched
> has the following columns: ...2) table_name: The table name..."  It is
> simply a Python list of these that I want, but the API for actually
> returning such is far from clear: how does one access the collection of
> "fetched rows" (what kind of Python object is the collection and what is
> its name in the local dictionary) and how does one "slice out" one or
> more columns from it?  Please help.  Thanks!

Not sure whether that's an option, but you could try our mxODBC
which uses standard tuples, lists, etc.:

    http://www.egenix.com/products/python/mxODBC/

Here's the documentation:

    http://www.egenix.com/products/python/mxODBC/mxODBC.pdf

or for online reading (but not as nicely formatted):

    http://www.egenix.com/products/python/mxODBC/doc/

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Oct 07 2010)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
_______________________________________________
DB-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig
Reply | Threaded
Open this post in threaded view
|

Re: pyodbc: help interpreting help

Goldsmith, David
Thanks, Marc-Andre.  That sounds interesting; unfortunately, I don't
have any money to spend. :-(

DG

-----Original Message-----
From: M.-A. Lemburg [mailto:[hidden email]]
Sent: Thursday, October 07, 2010 2:13 AM
To: Goldsmith, David
Cc: [hidden email]
Subject: Re: [DB-SIG] pyodbc: help interpreting help

Goldsmith, David wrote:
> Hi!  Probably 'cause I'm new to DBs, I'm having trouble understanding
> pyodbc's help.  For example, what kind of Python object is a "results
> set"?  In particular, the syntax portion of the docstring for the
tables
> method of a cursor object states that it returns self, but the text
> portion of the docstring says it "creates a results set of tables
> defined in the data source."  It goes on to say that "each row fetched
> has the following columns: ...2) table_name: The table name..."  It is
> simply a Python list of these that I want, but the API for actually
> returning such is far from clear: how does one access the collection
of
> "fetched rows" (what kind of Python object is the collection and what
is
> its name in the local dictionary) and how does one "slice out" one or
> more columns from it?  Please help.  Thanks!

Not sure whether that's an option, but you could try our mxODBC
which uses standard tuples, lists, etc.:

    http://www.egenix.com/products/python/mxODBC/

Here's the documentation:

    http://www.egenix.com/products/python/mxODBC/mxODBC.pdf

or for online reading (but not as nicely formatted):

    http://www.egenix.com/products/python/mxODBC/doc/

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Oct 07 2010)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
_______________________________________________
DB-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/db-sig