Could this have something to do with using a database in memory?  I
just noticed if I call create_all() within my class every time I want
to update the database it works.  Also it is able to add multiple
items by pressing refresh.  But when I browse away from the web page
and return later it resets the database in memory.  Perhaps this is a
problem with cherrypy and not sqlalchemy.

On Aug 4, 10:22 pm, kportertx <[email protected]> wrote:
> But this is all in one file, create_all() after all classes defining
> the model are loaded.
>
> I noticed earlier I accidentally posted the error twice, here is the
> source code.  Could you try running this and see if you run into the
> same problem.
>
> Again this all one file.  This is also my first week with sqlalchemy
> so if there happens to be something that I should change feel free to
> tell me.  I'm just trying to learn another way to '
> program for the web, been using PHP for years.  Generally I don't use
> ORM but this one caught my eye, and I thought I would give it a try.
>
> <BEGIN CODE>
>
> #!/usr/bin/env python
> ########################################################################
> #
> #
> #                               MODEL
> #
> #
> #
> ########################################################################
> import sqlalchemy
> from sqlalchemy import Table, Column, Integer, String, MetaData,
> ForeignKey
> from sqlalchemy.orm import relation, backref
> from sqlalchemy.orm import mapper
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy import create_engine
> from sqlalchemy.orm import sessionmaker
> from sqlalchemy.orm import mapper
>
> # engine is the connection to the database
> engine = create_engine('sqlite:///:memory:', echo=True)  # TODO: Move
> to a Conf file
>
> Base = declarative_base(bind=engine)
> class Test(Base):
>     """ ORM for Tests. """
>     __tablename__ = "tests"
>
>     id = Column(Integer, primary_key=True)
>     name = Column(String(50))
>
>     def __init__(self,name):
>         """ Accepts a name for a test """
>         #TODO: Ensure valid input
>         self.name = name
>
> class Question(Base):
>     """ ORM for Questions. """
>     __tablename__ = "questions"
>
>     id = Column(Integer, primary_key=True)
>     test_id = Column(Integer, ForeignKey("tests.id"))
>     question = Column(String)
>
>     test = relation(Test, backref=backref('questions', order_by=id))
>
>     def __init__(self,test_id, question):
>         """ Accepts a test_id and a question to sign that test """
>         #TODO: Ensure valid input
>
>         self.question = question
>         self.test_id = test_id
>
> class Answer(Base):
>     """ ORM for Answers. """
>     __tablename__ = "answers"
>
>     id = Column(Integer, primary_key=True)
>     question_id = Column(Integer, ForeignKey("questions.id"))
>     answer = Column(String)
>     correct = Column(String(1), default='n') # y for correct n for not
>
>     question = relation(Question, backref=backref('answers',
> order_by=id))
>
>     def __init__(self,question_id,answer,correct=0):
>         """ Accepts a question_id and a answer to assign to that
> question.
>             Optionally it accepts correct, y for correct, n for not
>             Default for correct is n"""
>         #TODO: Ensure vaild input
>
>         self.question_id = question_id
>         self.answer = answer
>         self.correct = correct
>
> def setup_model():
>     Base.metadata.create_all(engine) # Create all tables
>     # Session class is how we talk to the engine (database)
>     global Session
>     Session = sessionmaker(bind=engine)
>
> ########################################################################
> #
> #
> #                          VIEW/CONTROLLER
> #
> #                        Will Seperate Later
> #
> #
> #
> ########################################################################
>
> import cherrypy
>
> class WebTest(object):
>     """ Cherrypy server root """
>     def __init__(self):
>         setup_model()
>
>     def header(self):
>         """ Defines a header to be used for web pages... Should be
> temporary """
>         return "<html><head><title>pyWebTest</title></head><body>"
>
>     def footer(self):
>         """ Defines a footer to be used for web pages... Should also
> be temporary """
>         return "</body></html>"
>
>     @cherrypy.expose()
>     def index(self):
>         """ Root page for pyWebTest server """
>         output = 'Begin Test <br />'
>         session = Session()
>         test = Test("network+")
>         session.add(test)
>         output = output + str(session.new) + '<br />'
>         session.commit()
>         output = output + str(session.new) + '<br />'
>         print session.dirty
>         return self.header() + output + self.footer()
>
> def main():
>     cherrypy.quickstart(WebTest())
>     return 0
>
> if __name__ == '__main__': main()
>
> <END CODE>
--~--~---------~--~----~------------~-------~--~----~
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