[sqlalchemy] Re: Retrive datetime attributes

2012-06-21 Thread GHZ
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)


On Thursday, June 21, 2012 2:35:24 PM UTC+2, Fabien Ribes wrote:

 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 ?




-- 
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 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.



Re: [sqlalchemy] Re: Retrive datetime attributes

2012-06-21 Thread fribes
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)



On 21 June 2012 15:35, GHZ geraint.willi...@gmail.com wrote:

 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)


 On Thursday, June 21, 2012 2:35:24 PM UTC+2, Fabien Ribes wrote:

 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 ?


  --
 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 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.


-- 
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.