Wednesday, April 16, 2008

SQLAlchemy elixir: Simple example

DB Setup
[root@localhost ~]# yum -y install postgresql-python \
postgresql postgresql-server
[root@localhost ~]# /etc/init.d/postgresql start
[root@localhost ~]# /etc/init.d/postgresql status
[root@localhost ~]# su - postgres -c "createuser --createdb \
--adduser shon"
[root@localhost ~]# su - shon # normal user
[shon@localhost ]$ createdb test
[shon@localhost ]$ psql test
test=# \q


Code test_alchemy.py


from elixir import *

metadata.connect("postgres:///test")

class Movie(Entity):
has_field('title', Unicode(30))
has_field('year', Integer)
has_field('description', Unicode)

def __repr__(self): return '
' % (self.title, self.year)

metadata.create_all()

def test1():
m1 = Movie(title="Blade Runner", year=1982)
m2 = Movie(title="Life is beautiful", year=1980)
objectstore.flush()
print m1

def test2():
print Movie.select()[0]

test1()
# test2()

No comments:

Post a Comment