On Wed, Aug 06, 2008 at 01:05:02PM +0200, Petr Jake?? wrote:
> in my tables I am using timestamp field.
> I would like to select all records from the table that have the same date
> (not the same timestamp!!), say all records with the date 31.12.2007. What
> is the best way to do it in the SQLObject?

   Using 'date()' function in SQL (not in SQLObject):

from sqlobject import *
from sqlobject.sqlbuilder import *

__connection__ = "sqlite:/:memory:?debug=1"

from datetime import datetime

class Test(SQLObject):
   ts = DateTimeCol()

Test.createTable()

Test(ts=datetime.strptime('2008-08-01 21:44:33', '%Y-%m-%d %H:%M:%S'))

print list(Test.select(func.date(Test.q.ts) == '2008-08-01'))

 2/QueryIns:  INSERT INTO test (ts) VALUES ('2008-08-01 21:44:33')
 2/QueryR  :  INSERT INTO test (ts) VALUES ('2008-08-01 21:44:33')
 3/QueryOne:  SELECT ts FROM test WHERE ((test.id) = (1))
 3/QueryR  :  SELECT ts FROM test WHERE ((test.id) = (1))
 4/Select  :  SELECT test.id, test.ts FROM test WHERE ((date(test.ts)) = 
('2008-08-01'))
 4/QueryR  :  SELECT test.id, test.ts FROM test WHERE ((date(test.ts)) = 
('2008-08-01'))
[<Test 1 ts='datetime.datetime...)'>]

   'func' is SQLBuilder's magic that passes its attribute to SQL unchanged.
Note 'date(test.ts)'! You should check that your backend actually
implements the function (for 'date' most do).

Oleg.
-- 
     Oleg Broytmann            http://phd.pp.ru/            [EMAIL PROTECTED]
           Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to