[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Python ADO Date Time database fields

goldtech

1/24/2008 11:56:00 PM

Hi,

Given an MS-Access table with a date type field with a value of:
12:00:00 AM - just"12:00:00 AM", there's nothing else in the field.

I want to print exactly what's in the field, ie. "12:00:00 AM". What I
get printed is: 12/30/0/ 00:00:00

I try:

import win32com.client
from win32.client import Dispatch

oConn=Dispatch('ADODB.Connection')
db = r'C:\mydb.mdb'
oConn.Open("Provider=Microsoft.Jet.OLEDB.4.0; data Source="+db)
oRS = Dispatch('ADODB.RecordSet')
oRS.ActiveConnection = oConn
c = oConn.OpenSchema(20)

while not c.EOF:
tn = c.Fields.Item('Table_Name').Value
oRS.Open(tn)
(oRS, dt) = oConn.Execute('SELECT date_field FROM '+tn+' GROUP BY
date_field')
while not oRS.EOF:
print oRS.Fields(dt).Value # print here
oRS.MoveNext()
c.MoveNext()

oRS.Close()
oConn.Close()
# end

What I get printed is: 12/30/0/ 00:00:00

What do I to get exactly what's in the field?

Thanks,

Lee G
7 Answers

Mike Driscoll

1/25/2008 1:04:00 AM

0

On Jan 24, 5:55 pm, goldtech <goldt...@worldpost.com> wrote:
> Hi,
>
> Given an MS-Access table with a date type field with a value of:
> 12:00:00 AM - just"12:00:00 AM", there's nothing else in the field.
>
> I want to print exactly what's in the field, ie. "12:00:00 AM". What I
> get printed is: 12/30/0/ 00:00:00
>
> I try:
>
> import win32com.client
> from win32.client import Dispatch
>
> oConn=Dispatch('ADODB.Connection')
> db = r'C:\mydb.mdb'
> oConn.Open("Provider=Microsoft.Jet.OLEDB.4.0; data Source="+db)
> oRS = Dispatch('ADODB.RecordSet')
> oRS.ActiveConnection = oConn
> c = oConn.OpenSchema(20)
>
> while not c.EOF:
> tn = c.Fields.Item('Table_Name').Value
> oRS.Open(tn)
> (oRS, dt) = oConn.Execute('SELECT date_field FROM '+tn+' GROUP BY
> date_field')
> while not oRS.EOF:
> print oRS.Fields(dt).Value # print here
> oRS.MoveNext()
> c.MoveNext()
>
> oRS.Close()
> oConn.Close()
> # end
>
> What I get printed is: 12/30/0/ 00:00:00
>
> What do I to get exactly what's in the field?
>
> Thanks,
>
> Lee G

I don't know for sure, so I practiced my Google-Fu and found this
recipe which might be of use to you:

http://aspn.activestate.com/ASPN/Cookbook/Python/Re...

I also found this script, which might help you interface better with
Access:

http://www.ecp.cc/...

And then there just connecting to it with the adodb module:

http://phplens.com/lens/adodb/adodb-p...

Mike

John Machin

1/25/2008 9:29:00 AM

0

On Jan 25, 10:55 am, goldtech <goldt...@worldpost.com> wrote:
> Hi,
>
> Given an MS-Access table with a date type field with a value of:
> 12:00:00 AM - just"12:00:00 AM", there's nothing else in the field.
>
> I want to print exactly what's in the field, ie. "12:00:00 AM". What I
> get printed is: 12/30/0/ 00:00:00
>
> I try:
[snip]
> print oRS.Fields(dt).Value # print here

try this:

val = oRS.Fields(dt).Value
print type(val)
print float(val)

If the last one gives you 0.0, then you have got exactly what's in the
database -- stored as a fraction of a day. Six AM would give you 0.25.

Converting that to 24 hour clock is easy:
>>> val = 0.12345
>>> seconds = int(round(val * 60 * 60 * 24))
>>> minutes, second = divmod(seconds, 60)
>>> hour, minute = divmod(minutes, 60)
>>> '%02d:%02d:%02d' % (hour, minute, second)
'02:57:46'
>>> ((((46/60.)+57)/60.)+2)/24. # checking
0.12344907407407407

If you don't get 0.0, let us know what you did get.

HTH,
John

goldtech

1/25/2008 1:48:00 PM

0

snip
>
> try this:
>
> val = oRS.Fields(dt).Value
> print type(val)

this gives: <type 'time'>



> print float(val)

yes, it gives 0.0

But there should be a way to print what is *actually in the field*.
When I open the DB table in Access I see: 12:00:00 AM.

That's what I want - the value, and the form of the value, exactly as
seen in the field...

As an aside, the roughly eqivalent code in Perl will print the
"12:00:00 AM" - (the trick for the date types in Perl is to add: "use
Win32::OLE::Variant;"

There has to be a way:^)

snip

Mike Driscoll

1/25/2008 1:54:00 PM

0

On Jan 25, 7:48 am, goldtech <goldt...@worldpost.com> wrote:
> snip
>
>
>
> > try this:
>
> > val = oRS.Fields(dt).Value
> > print type(val)
>
> this gives: <type 'time'>
>
> > print float(val)
>
> yes, it gives 0.0
>
> But there should be a way to print what is *actually in the field*.
> When I open the DB table in Access I see: 12:00:00 AM.
>
> That's what I want - the value, and the form of the value, exactly as
> seen in the field...
>
> As an aside, the roughly eqivalent code in Perl will print the
> "12:00:00 AM" - (the trick for the date types in Perl is to add: "use
> Win32::OLE::Variant;"
>
> There has to be a way:^)
>
> snip

You could try posting to the PyWin32 group too. They would probably
know.

http://mail.python.org/mailman/listinfo/py...

Mike

John Machin

1/25/2008 8:58:00 PM

0

On Jan 26, 12:48 am, goldtech <goldt...@worldpost.com> wrote:
> snip
>
>
>
> > try this:
>
> > val = oRS.Fields(dt).Value
> > print type(val)
>
> this gives: <type 'time'>
>
> > print float(val)
>
> yes, it gives 0.0
>
> But there should be a way to print what is *actually in the field*.

What is "actually in the field" is a bunch of bits -- in this case all
zero. What you *see* is quite another matter.

> When I open the DB table in Access I see: 12:00:00 AM.

And if you were to close Access, go Start / Control Panel / Regional
and Language settings, change the Time display format to HH:mm:ss, and
go back to look at your database, you'd see 00:00:00 ... send a copy
of your database to someone in another country and they'd likely see
something else again.

>
> That's what I want - the value, and the form of the value, exactly as
> seen in the field...

>
> As an aside, the roughly eqivalent code in Perl will print the
> "12:00:00 AM" - (the trick for the date types in Perl is to add: "use
> Win32::OLE::Variant;"
>
> There has to be a way:^)

C:\junk>type goldtech.py
def time_format_12(day_fraction):
assert 0.0 <= day_fraction < 1.0
seconds = int(day_fraction * 60 * 60 * 24)
minutes, second = divmod(seconds, 60)
hour, minute = divmod(minutes, 60)
if hour >= 12:
tag = 'PM'
else:
tag = 'AM'
hour12 = (hour - 1) % 12 + 1
return '%02d:%02d:%02d %s' % (hour12, minute, second, tag)

if __name__ == "__main__":
import sys
args = sys.argv[1:]
if args:
tests = map(float, args)
else:
tests = (
[0.0, 0.5, 0.9999999]
+ [(h + 0.99) / 24.0 for h in (0, 1, 11, 12, 13, 23)]
)
for test in tests:
print "%8.6f %s" % (test, time_format_12(test))

C:\junk>goldtech.py
0.000000 12:00:00 AM
0.500000 12:00:00 PM
1.000000 11:59:59 PM
0.041250 12:59:24 AM
0.082917 01:59:24 AM
0.499583 11:59:23 AM
0.541250 12:59:24 PM
0.582917 01:59:24 PM
0.999583 11:59:23 PM

C:\junk>goldtech.py 0.1 0.01 0.001
0.100000 02:24:00 AM
0.010000 12:14:24 AM
0.001000 12:01:26 AM

C:\junk>

Gray Ghost

7/27/2014 4:12:00 PM

0

GOP_Decline_and_Fall <Dev@null.net> wrote in
news:d28at91qgmo8d957ttqidpp7fn3eg0io17@4ax.com:

> On Sun, 27 Jul 2014 13:59:26 +0000 (UTC), Thomas Paine
><grey_ghost471-newsgroups@yahoo.com> wrote:
>
>>GOP_Decline_and_Fall <Dev@null.net> wrote in
>>news:r3f8t99bilsj94dosoh6vqv397qhits4dp@4ax.com:
>>
>>>
>>> Blinded by hatred, his ravings are informed more by his xenophobia and
>>> blood lust than history.
>>>
>>> Not exactly a pretty sight is it?
>>>
>>
>>Yes and your antisemitism
>
> Oh..what anti-Semitism would that be?
>
>>and support for the bloody murders,
>
> Project much?
>
> Incitement to and support of bloody murders is your daily routine as
> all who have had the dubious pleasure of reading your psychotic
> postings are well aware.
>
>>what does that say about you?
>
> AFIAC a UN peacekeeping force should have been there weeks ago.
>
>>To me it says you are not fit for a polite and civilized society.
>
> You demonstrate everyday that you don't know the first thing about
> either being civilized or politeness.
>
>
>
>
>

Fuck off. I look forward to viewing your autopsy photos.

You have gotten to the point where I am bored enough that were we in
proximity it would not go well for you. You do not appear to have the
smallest redeeming quality.


--
When the government is no longer constrained by the laws of the land, then
neither are the people.

Big Muddy

7/27/2014 8:05:00 PM

0

On 7/25/2014 11:03 PM, GOP_Decline_and_Fall wrote:
> Abbas said that at Aqaba,
YOUR mission here is fatally compromised Sheila, ruined if you will.

Sheila, I think it's time to chronicle what your game plan is here in
the uselessnet:

1.) You're a paid DNC hack troll.

2.) Your mission statement is to enter various threads and then ignite
heated debate.

3.) Your tactic is to at first argue topically, but then locate a point
which is unsupportable.

4.) Having identified said point you are charged to see if by supporting
it you can anger conservative posters.

5.) If this succeeds, you are charged to argue ad absurdism until said
conservative posters curse you out for your evident and endless stupidity.

6.) At that point you are charged with demonizing your detractors for
their anger and painting them as representative of Republicans at large.

7.) When the topic becomes too lengthy or the insults dominate, you are
charged with slinking away for a time, even weeks, before returning to
ignite another shit-storm with your support of the unsupportable.

That is who and what you are Sheila, and it is the reason you went to
lengths of absurdity heretofore unseen in trying to maintain that a TV
has no moving parts.

Your mission is now identified for all and thereby corrupted.

What will your DNC handlers task you with next?

Cuz everyone here knows who you are, what you do, and who pays you to do it.

Game over, DNC shill...game fucking over.