On 2/6/07, Steve Bergman <[EMAIL PROTECTED]> wrote:
>
> Hi guys,
>
> I'm brand new to Pylons, coming from TurboGears.
>
> In TG it is drop dead simple to get the model defined, the database
> tables auto-generated, and to the point that I'm ready to start
> writing controllers and templates.
>
> You just:
>
> 1. $ tg-admin quickstart hellodatabase
>
> 2. Define the model in models.py
>
> 3. tg-admin sql create
>
> and you're ready to start coding.
>
> I've done the QuickWiki tutorial.  And based upon that it seems that
> in Pylons there is more involved in getting to that point.  Config
> files to edit, etc.  Even defining the database URI in two separate
> places.
>
> That's not the end of the world, of course.  But I would be interested
> to know just what is the simplest way to get a quick and dirty project
> started.

I'm just figuring this out now.  The absolute minimum, assuming you
have an existing MySQL database like I do, is:

=====
# development.ini
sqlalchemy.dburi = mysql://USER:[EMAIL PROTECTED]/DB?use_unicode=1
sqlalchemy.echo = true

# A controller method
def simple_query(self):
        from pylons.database import create_engine
        engine = create_engine()
        sql = "SELECT name FROM Incident ORDER BY activity_date DESC LIMIT 1"
        data = list(engine.execute(sql))
        if data:
            name = data[0][0]
            return Response('Newest incident is <strong>"%s"</strong>.' % name)
        else:
            return Response("No incidents found.")
=====

Going beyond this, you'd have to set up a model and put
database-creation commands in My_APP/websetup.py.  The best info for
setting this up seems to be this mail message:

http://groups.google.com/group/pylons-discuss/msg/f424e9f51f7e3627

which is linked in the "Using SQLAlchemy with Pylons" wiki page:

http://pylonshq.com/project/pylonshq/wiki/SqlAlchemyWithPylons

Note that the wiki page is out of date.  I've never seen "dsn" or
"echo_queries" config vars, so they must come from an earlier version
of Pylons.

The most useful functions in pylons.database are documented here:
http://pylonshq.com/docs/0.9.4/module-pylons.database.html#make_session

engine = create_engine()
gets you an engine connection based on the config file.

db_session = make_session()
creates a SQLAlchemy session to access the ORM features.

QuickWiki puts the DB session in a 'self' attribute in the base
controller, if I remember right.

What do you mean about putting the database URI in two separate
places?    Where's the other place?

-- 
Mike Orr <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to