Hi,

i am fairly new to python, tkinter and sqlalchemy.
I try to edit values in a database using these tools.

My question is, what is going wrong with the following python3 program?
I am not sure, wether this is a question of python, sqlalchemy or tkinter.

#######################################################
#!/usr/bin/env python3
#-*- coding: utf-8 -*-

'''
Created on 20.06.2012

@author: wolfgang
'''

from sqlalchemy import String, Integer, Column, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.session import sessionmaker

from tkinter import *
from tkinter.ttk import *


Base = declarative_base()

class Mytable(Base):
    __tablename__ = 'mytable'

    myid = Column(Integer, primary_key=True)
    value = Column(String)

    def __init__(self, value=''):
        self.value = value

class TableEntry(Frame):
    def __init__(self,
                 root=None,
                 entry=None,
                 varList=None,
                 action_name='print',
                 action=lambda t: print(t)):
        '''
        root = the tkinter frame that contains TableEntry
        entry: a class instance that holds the entry to view
        varlist: a list of values which are contained in vars(entry)
        action_name: the string which is shown on the action button
        action: an action that is performed when the action button is
        pressed
        '''
        if root is None:
            Frame.__init__(self, master=Toplevel())
            self.pack(side=TOP, expand=YES, fill=BOTH)
        else:
            Frame.__init__(self, master=root)
            # dont forget to call geometry manager

        self.entry = entry
        self.action = action

        self.entries = []
        i = 0
        for field in varList:
            Label(self, text=field).grid(row=i, column=0)
            e = Entry(self)
            s = vars(entry)[field]
            if s: e.insert(0, s)
            e.grid(row=i, column=1)
            self.entries.append((field,e))
            i += 1

        Button(self, text=action_name,
               command=self.onAction).grid(row=i, column=1)

    def onAction(self, *args):
        for (field, e) in self.entries:
            vars(self.entry)[field] = e.get()
        self.action()
        self.master.destroy()

if __name__ == '__main__':
    #make database
    dbname = 'sqlite:///mytest.sqlite'
    engine = create_engine(dbname)
    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)
    Session = sessionmaker(engine)
    session = Session()

    session.add(Mytable(value='A'))
    session.commit()
    # now, there is exactly one entry with value = 'A

    myentry=session.query(Mytable).first()
    print('1 %s' % vars(myentry))
    # prints this value

    MyFrame = TableEntry(root=Tk(),
                         entry=myentry,
                         varList=['value'],
                         action_name='print',
                         action = \
                         lambda t = myentry : print('2 %s' % vars(t)))
    MyFrame.pack(side=TOP, expand=YES, fill=BOTH)
    MyFrame.mainloop()
    # change the value from 'A' to 'B'

    print('3 %s' % vars(myentry))
    # now, the value of myentry has changed from 'A' to 'B'

    session.commit()
    # write changed value to database. Does not work

    myentry = session.query(Mytable).first()
    print('4 %s' % vars(myentry))
    # still the old value 'A', not 'B'
#####################################################################

the output of this program is
1 {'myid': 1, '_sa_instance_state': <sqlalchemy.orm.state.InstanceState
object at 0x1011aabd0>, 'value': 'A'}
2 {'myid': 1, '_sa_instance_state': <sqlalchemy.orm.state.InstanceState
object at 0x1011aabd0>, 'value': 'B'}
3 {'myid': 1, '_sa_instance_state': <sqlalchemy.orm.state.InstanceState
object at 0x1011aabd0>, 'value': 'B'}
4 {'myid': 1, '_sa_instance_state': <sqlalchemy.orm.state.InstanceState
object at 0x1011aabd0>, 'value': 'A'}

at point 1, one row with value 'A' has been added to the database.
at point 2, this value has been changed to 'B'
at point 3, the value of myentry is 'B'
at point 4, it can be seen, that this value has not been written to the
database.

So how is this done correctly?
Thank you for any hints.

Wolfgang

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

Reply via email to