Query about using "DataHandlers" module

Andy Elvey andy.elvey at paradise.net.nz
Sun Jun 4 19:13:01 EDT 2000


Oops ...... the "example_07b" that I posted didn't quite show what I meant
with my question .....

 The code attached here *does* show what I mean..... it has the SQL code in it
with the WHERE statement that doesn't work .....

Andy Elvey wrote:

> Hi all.  I have a query about the use of the DataHandlers module
> (available at "Vaults of Parnassus").
>
>  I have attached a simple  example which uses this module (this example
> works fine).  But .... when I amend the SQL code to say  'select * from
> test where team == "Yankees" '  ,  the code still retrieves all teams -
> the where statement is ignored.
>
>   My question is - "does anyone have an example (or two!) of some Python
> code similar to my example , but which has a select statement that has a
> "where" clause in it?
>
>  ( and -  are there any examples which use the DataHandlers module and
> use * 2 tables*   with a join ... ? )
>
>  Many thanks in advance for any help offered .... :-)
>
>  ( and by the way .. this DataHandlers module is *great*  ..... :-)
>
>   ------------------------------------------------------------------------
> #!/usr/bin/env python
>
> # Illustrate the parsing a data file and creating SQL insert statements
> # The table definition comes from an XML file
>
> from DataHandlers.SQLTable import SQLTable
> from DataHandlers.dbdefXMLParser import dbdefXMLParser
> from DataHandlers.FileConnection import Connection
>
> # create a parser
> xml = dbdefXMLParser()
> # return a dictionary of table definitions
> tables = xml.parseFile( 'test.xml' )
> # get the baseball table
> tbl = tables['baseball']
> # the data file is delimited by commas
> tbl.setReader( 'Delimited' )
> tbl.setDelimiter( ',' )
> # open a file connection
> fc = Connection()
> fc.connect( 'teams.txt' )
> # we need to do this fake query so we can get the cursor
> # it actually means something when we are doing an SQL thing
> cursor = fc.query('')
> if not cursor:
>     print 'Unable to execute query'
>     sys.exit(1)
> # now walk thru the lines of data
> while cursor.next():
>     tbl.parse( cursor.getData())
>     # print out the insert statements
>     print tbl.getInsert()
>
> print "Query data returned..."
> cursor = fc.query( 'select * from test' )
> while cursor.next():
>     print cursor.getData( )
>
> cursor.close()
>
>
>   ------------------------------------------------------------------------
> <dbdef>
>   <sqltable name="baseball">
>     <field name="team" size="20" type="char" />
>     <field name="city" size="20" type="char" />
>     <field name="stadium" size="30" type="char" />
>     <field name="league" size="1" type="char" />
>     <field name="division" size="1" type="char" />
>   </sqltable>
> </dbdef>
>
>   ------------------------------------------------------------------------
> Orioles,Baltimore,Camden Yards,A,E
> Red Sox,Boston,Fenway Park,A,E
> Angels,Anaheim,Anaheim Stadium,A,W
> White Sox,Chicago,Comiskey Park,A,W
> Indians,Cleveland,Jacobs Field,A,E
> Tigers,Detroit,Tiger Stadium,A,E
> Royals,Kansas City,Royals Stadium,A,W
> Brewers,Milwaukee,County Stadium,A,E
> Twins,Minnesota,Hubert H. Humphrey Stadium,A,W
> Yankees,New York,Yankee Stadium,A,E
> Athletics,Oakland,Oakland Coliseum,A,W
> Mariners,Seattle,Kingdome,A,W
> Rangers,Texas,Arlington Stadium,A,W
> Blue Jays,Toronto,Toronto Stadium,A,E
> Braves,Atlanta,Fulton County Stadium,N,W
> Cubs,Chicago,Wrigley Field,N,E
> Reds,Cincinnati,Three Rivers Stadium,N,W
> Astros,Houston,Astrodome,N,W
> Dodgers,Los Angeles,Dodger Stadium,N,W
> Expos,Montreal,Exposition Stadium,N,E
> Mets,New York,Shea Stadium,N,E
> Phillies,Philadelphia,Veterans Stadium,N,E
> Pirates,Pittsburgh,Pirate Stadium,N,E
> Cardinals,St. Louis,Busch Stadium,N,E
> Padres,San Diego,Jack Murphy Stadium,N,W
> Giants,San Francisco,Candlestick Park,N,W
-------------- next part --------------
#!/usr/bin/env python

# Illustrate the parsing a data file and creating SQL insert statements
# The table definition comes from an XML file

from DataHandlers.SQLTable import SQLTable
from DataHandlers.dbdefXMLParser import dbdefXMLParser
from DataHandlers.FileConnection import Connection

# create a parser
xml = dbdefXMLParser()
# return a dictionary of table definitions
tables = xml.parseFile( 'test.xml' )
# get the baseball table
tbl = tables['baseball']
# the data file is delimited by commas
tbl.setReader( 'Delimited' )
tbl.setDelimiter( ',' )
# open a file connection
fc = Connection()
fc.connect( 'teams.txt' )
# we need to do this fake query so we can get the cursor
# it actually means something when we are doing an SQL thing

print "Query data returned..."
cursor = fc.query( 'select * from test where team eq "Yankees"' )
if not cursor:
    print 'Unable to execute query' 
    sys.exit(1)
while cursor.next():
    tbl.parse( cursor.getData())
    print cursor.getData( )

cursor.close()
    


More information about the Python-list mailing list