Hi Michael,

many thanks for your help!

Is there any way to do this in an automatic way? I mean now i have the 
following code


from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

MaPMTs = Table('MaPMTs',Base.metadata,
               Column('id',Integer,primary_key=True),
               Column('Serial_Number',String),
               Column('Gain',Float),
               Column('Validity_Start',DateTime,default=func.now()),
               Column('Validity_End',DateTime),
               )

class MaPMTs(Base):
    __table__ = MaPMTs

from sqlalchemy import create_engine
engine = create_engine('postgres://localhost/DB')

from sqlalchemy.orm import sessionmaker
DBSession = sessionmaker()
DBSession.configure(bind=engine)



and i obtain for example this table 

ID             Serial_Number              Gain                            
Validity_Start                                                      Validity_End

1;               “FA0026”;                     1.18;                        
"2014-11-27 11:16:36.779973”;                         "2014-11-27 
11:18:44.91704"
2;               ”FA0026”;                     1.08;                        
"2014-11-27 11:18:44.91704";""



typing  on shell 

>>> s=DBSession()
>>> tube1=MaPMTs(Serial_Number='FA0026',Gain=1.18)
>>> s.add(tube1)
>>> s.commit()
>>> tube2=MaPMTs(Serial_Number='FA0026',Gain=1.08)
>>> s.add(tube2)
>>> s.commit()
>>> tube1_update = 
>>> s.query(MaPMTs).filter(MaPMTs.Serial_Number=='FA0026').first()
>>> tube1_update.Validity_End = tube2.Validity_Start
>>> s.commit()



I would obtain the same table without invoke a query. Is it possibile to add 
some new lines in the code to do the same thing?

Giovanni




> Il giorno 27/nov/2014, alle ore 01:18, Michael Bayer 
> <mike...@zzzcomputing.com> ha scritto:
> 
> idiomatically this would be:
> 
> tube1 = session.query(Phototubes).filter(<get the first object>).one()
> tube2 = Phototubes(gain = new_gain, transactiondate = tube1.transactiondate, 
> serialnumber=tube1.serialnumber)
> session.add(tube2)
> session.commit()
> 
> 
> That’s a SELECT then an INSERT.  In the event that you’re looking for 
> something more exotic than that (like, an INSERT that embeds the SELECT)  let 
> us know.
> 
> 
>> On Nov 26, 2014, at 5:37 AM, joecav...@hotmail.it 
>> <mailto:joecav...@hotmail.it> wrote:
>> 
>> Hello,
>> 
>> i'm new on sqlalchemy and i have a problem. 
>> I want to update the entry in a column of a table of a row using the info of 
>> a new row. For example, i could have measured a value in a certain date
>> 
>> class Phototubes(Base):
>>     __tablename__ = 'MaPMTs'
>>     SerialNumber = Column(String, primary_key=True)
>>     Gain = Column(Float)
>>     TransactionDate = Column(
>>         DateTime,
>>         default=func.now())
>>     )
>>  
>> Now, I would add a column "End_Of_Validity = Column(DateTime)" so that if I 
>> measure a new value of the Gain of the same phototube (so I add a new row 
>> with the same SerialNumber but different value of Gain) I can update the 
>> previous entry of column End_Of_Validity with this TransactionDate 
>> (otherwise empty). How can I do this?     
>> Many Thanks,
>> Giovanni                          
>> 
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "sqlalchemy" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to sqlalchemy+unsubscr...@googlegroups.com 
>> <mailto:sqlalchemy+unsubscr...@googlegroups.com>.
>> To post to this group, send email to sqlalchemy@googlegroups.com 
>> <mailto:sqlalchemy@googlegroups.com>.
>> Visit this group at http://groups.google.com/group/sqlalchemy 
>> <http://groups.google.com/group/sqlalchemy>.
>> For more options, visit https://groups.google.com/d/optout 
>> <https://groups.google.com/d/optout>.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com 
> <mailto:sqlalchemy+unsubscr...@googlegroups.com>.
> To post to this group, send email to sqlalchemy@googlegroups.com 
> <mailto:sqlalchemy@googlegroups.com>.
> Visit this group at http://groups.google.com/group/sqlalchemy 
> <http://groups.google.com/group/sqlalchemy>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to