logo       

Re: Getting the time during a transaction: msg#00012

python.db.pysqlite.user

Subject: Re: Getting the time during a transaction

Hi Tim. It was a surprise to me as well! The thing that screwed me up
was assert cursor.rowcount. Hmm, it should be 1 since I am getting a
result from .fetchone but is reporting -1. I incorrectly assumed I was
getting nothing from my select statement. Any idea about this? Why is it
not giving me a rowcount of 1 so my assertion is correct?

cursor.execute(stmt)
print cursor.rowcount
#assert cursor.rowcount == 1
last_tid, now = cursor.fetchone()

Your example here is super awesome BTW since it shows this great
functionality I did not know existed. In this particular instance, I
can put this together in a very simple way. Many thanks.

Regards,
David


Tim Golden wrote:
> David Pratt wrote:
>> This seems to work fine after all..
>>
>> select tid, current_timestamp from ...
>>
>>
>> David Pratt wrote:
>>> Hi, I am continuing to through a postgres to sqlite conversion and have
>>> run into this sort of thing.
>>>
>>> select tid, extract(epoch from current_timestamp) ...
>>>
>>> extract(epoch from current_timestamp) is postgres functionality but is
>>> there a way to obtain the current_timestamp or similar from sqlite
>>> (where I can then obtain epoch with my program logic instead)? I need to
>>> use this for time comparisons but without storing it. Many thanks.
>
>
> Well I was just ignorantly working through a worked example
> of how to bind Python functions into the database, thinking
> that there was no current_timestamp functionality in sqlite.
> You've just proved me wrong, but I append it below as perhaps
> it might be useful.
>
> ------------------------
>
> The good news is that pysqlite has this *fantastically easy*
> ability to bind in your own functions which will then work
> seamlessly at the database level.
>
> Let's define epoch () as returning the year of a timestamp
> in time.ctime format, and bind it and some now function
> into a database connection:
>
> <code>
> import time
>
> def epoch (timestamp):
> """Return the year from a Python ctime value"""
> return time.strptime (timestamp)[0]
>
> from pysqlite2 import dbapi2 as sqlite
>
> db = sqlite.connect (":memory:")
> db.create_function ("now", 0, time.ctime)
> db.create_function ("epoch", 1, epoch)
>
> for row in db.execute ("SELECT now ()"): print row
>
> for row in db.execute ("SELECT epoch (now ())"): print row
>
> </code>
>
> TJG
> _______________________________________________
> pysqlite mailing list
> pysqlite-IAPFreCvJWPBWskQ1e/+sw@xxxxxxxxxxxxxxxx
> http://lists.initd.org/mailman/listinfo/pysqlite
>


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise