Database Help For Newbie - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Database Help For Newbie

bquad20bquad20 Posts: 17Member
I am trying to select * from a database table and format the
"SQL Time Stamp" in the list I am building using a for loop.
When I run the code below, I get
for the START_TIME and END_TIME.

My question(s):
- Is there way to format the date? If so, how?
AND/OR
- Is there a way to select individual fields from the
result set, not just fetchall() or fetchmany(x)?

code

import dbi, odbc

global jobsD
jobsD = []

def parseItDAO():
try:
conn = odbc.odbc('myOracle/user/pass')
c = conn.cursor()
sql = "select field1,START_TIME,END_TIME,field4 from table_name"
c.execute(sql)

#for x in c.fetchall():
for x in c.fetchmany(5):
jobsD.append(x)

conn.commit()
c.close()
conn.close()

return jobsD

except NameError,e:
print 'error ', e

Comments

  • infidelinfidel Posts: 2,900Member
    : I am trying to select * from a database table and format the
    : "SQL Time Stamp" in the list I am building using a for loop.
    : When I run the code below, I get
    : for the START_TIME and END_TIME.
    :
    : My question(s):
    : - Is there way to format the date? If so, how?
    : AND/OR
    : - Is there a way to select individual fields from the
    : result set, not just fetchall() or fetchmany(x)?
    :
    : code
    :
    : import dbi, odbc
    :
    : global jobsD
    : jobsD = []
    :
    : def parseItDAO():
    : try:
    : conn = odbc.odbc('myOracle/user/pass')
    : c = conn.cursor()
    : sql = "select field1,START_TIME,END_TIME,field4 from table_name"
    : c.execute(sql)
    :
    : #for x in c.fetchall():
    : for x in c.fetchmany(5):
    : jobsD.append(x)
    :
    : conn.commit()
    : c.close()
    : conn.close()
    :
    : return jobsD
    :
    : except NameError,e:
    : print 'error ', e

    I'm not familiar with the odbc module. When connecting to Oracle databases I use cx_Oracle. As far as formatting a DbiDate object, you'll have to look at either the dbi or odbc module documentation. There's probably some kind of function in that class to format or convert a date value to something else. Or you can try the dir() and help() functions (by passing them an object of type DbiDate) to see if anything useful prints out.

    The fetch methods of a cursor object return a list of tuples. Each tuple represents a record, so each item in the tuple represents a field in that record. You can't refer to a single field's value using the fetch methods (you'd be referring to a column rather than a field). If, say, you wanted all of the start dates from your data, you can do things like this:

    [code]
    start_times = []
    for r in c.fetchall():
    start_times.append(r(1))
    [/code]

    or

    [code]
    start_times = [r(1) for r in c.fetchall()]
    [/code]




    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

Sign In or Register to comment.