Hi All,

After some additional peeking around I decided to do a test with SQLAlchemy alone.

I took the tutorial fr0m the book Essential SQLAlchemy as my guide.

This is what I got working.

# testing the func following the tutorial in the book Essential SQLALchemy

#pg.25
from sqlalchemy import *
from datetime import datetime

metadata=MetaData('sqlite:///tutorial.sqlite')
#metadata.bind.echo=True

person_table=Table(
             'person', metadata,
             Column('id', Integer, primary_key=True),
             Column('birthdate', DateTime, default=datetime.now))

metadata.create_all()
#
stmt=person_table.insert()

#stmt.execute(birthdate=datetime(2000, 4, 4, 0, 0))
#stmt.execute(birthdate=datetime(2000, 3, 3, 0, 0))
#stmt.execute(birthdate=datetime(2000, 2, 2, 0, 0))
#stmt.execute(birthdate=datetime(2000, 1, 1, 0, 0))

#pg 28 Mapping
from sqlalchemy.orm import *

class Person(object):pass

mapper(Person, person_table)

Session= sessionmaker()
session=Session()

query =session.query(Person)

def monthfrom(date):
    print(date)
    #i do a split because I get seconds as 00.00000
    if date != None:
        datesplit=date.split(' ')[0]
        a=datetime.strptime(datesplit, '%Y-%m-%d').month
    else:
        a=1
    return a

metadata.bind.connect().connection.connection.create_function("monthfrom", 1, monthfrom)

print('monthfrom in:')
pp=query.order_by(func.monthfrom(Person.birthdate)).all()
print('result:')
for p in pp:
    print (p.birthdate)




For the first run you have to uncomment the 4 lines with the insert execution to fill your empty db.
Put the comments back on or your db will fill up


Next task will be to get it transplanted to my app.


There is still one issue though.

the function def monthfrom get the date with the seconds as 00.000000
But the print(p.birthdate) shows the seconds as 00
See:

monthfrom in:
2000-04-04 00:00:00.000000
2000-03-03 00:00:00.000000
2000-02-02 00:00:00.000000
2000-01-01 00:00:00.000000
result:
2000-01-01 00:00:00
2000-02-02 00:00:00
2000-03-03 00:00:00
2000-04-04 00:00:00

That is why the def monthfrom does a .split

Question is this a bug?


Thanks

Frans.


--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to