Retrive datetime attributes

55 views
Skip to first unread message

fribes

unread,
Jun 21, 2012, 8:35:24 AM6/21/12
to sqlal...@googlegroups.com
Hi all,

I'm using Python 2.6.5 and SQLAlchemy-0.7.8 over sqlite3 to store and retrieve logs with in table like this :

class LogEntry(Base):
    """Log class"""
   
    __tablename__ = 'log'
   
    #common data
    id = Column(Integer, primary_key=True)
    timestamp = Column(DateTime())

When querying back object, how comes I get unicode string in timestamp attribute ? Isn't SA supposed to convert ISO formatted string stored in sqlite back to python datetime object ?


GHZ

unread,
Jun 21, 2012, 9:35:14 AM6/21/12
to sqlal...@googlegroups.com
Here is code that works for me:


from datetime import datetime
from sqlalchemy import Column, DateTime, Integer, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite://', echo=True)

Base = declarative_base(engine)

Session = sessionmaker()
session = Session()

class LogEntry(Base):
    """Log class"""

    __tablename__ = 'log'

    #common data
    id = Column(Integer, primary_key=True)
    timestamp = Column(DateTime)

    def __init__(self):
        self.timestamp = datetime.now()

log = LogEntry()

Base.metadata.create_all()

session.add(log)
session.flush()

log = session.query(LogEntry).one()

print type(log.timestamp)

fribes

unread,
Jun 21, 2012, 10:34:26 AM6/21/12
to sqlal...@googlegroups.com
Thanks!

Here is a slightly  modified version that shows what happens : if querying in another session, with a from_statement, the string is not processed. Is it the expected behaviour ?

from datetime import datetime
from sqlalchemy import Column, DateTime, Integer, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite://', echo=True)

Base = declarative_base(engine)

Session = sessionmaker()


class LogEntry(Base):
    """Log class"""

    __tablename__ = 'log'

    #common data
    id = Column(Integer, primary_key=True)
    timestamp = Column(DateTime)

    def __init__(self,timestamp):
        self.timestamp = timestamp

log = LogEntry(timestamp=datetime.now())

Base.metadata.create_all()

session = Session()
session.add(log)
session.commit()
session.close()

session = Session()
log_1 = session.query(LogEntry).one()
session.close()

session = Session()
log_2 = session.query(LogEntry).from_statement("select * from log").one()
session.close()

print type(log_1.timestamp) 
print type(log_2.timestamp) 



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

Reply all
Reply to author
Forward
0 new messages