Skip to content
jmohr edited this page May 24, 2011 · 3 revisions

Getting Started

Hello, and welcome to the wide world of ORM madness. Inside, you will find tables, and rows with stuff in them, and also some stuff.

Installing Conrad

It's pretty easy to install Conrad. Conrad was designed to work with Python 2.7, but it may work with Python 3 without too much work. At some point I may try it.

Anyway, the recommended way to install Conrad is to 'pip' it up!

Installing with PIP

pip install conrad

Cloning from Github

This step requires you to have git. Imagine that.

git clone https://github.com/jmohr/conrad
cd conrad
python setup.py install

Get it from pypi

If for whatever reason the above two don't work, you can download the source bundle from pypi:

wget http://pypi.python.org/pypi/conrad
tar xvzf conrad.<version>.tar.gz
cd conrad
python setup.py install

Using Conrad

This part is pretty straight forward, too. Assuming you have an ODBC DSN configured on your system named MyDSN, with the following schema:

CREATE TABLE artist (
  id INTEGER PRIMARY KEY,
  name STRING NOT NULL
);

CREATE TABLE album (
  id INTEGER PRIMARY KEY,
  title STRING NOT NULL,
  artist_id INTEGER,
  FOREIGN KEY(artist_id) REFERENCES artist(id)
);

Connecting to a Database

Enter the python prompt, and do the needful:

>>> import conrad
>>> db = conrad.Database('MyDSN')

That's all you need to do to connect to a database. You can supply other arguments in the DSN as needed, and these will be passed to the ODBC connect method.

Creating records

First, let's create an artist.

>>> artist = db['artist']
>>> artist.create(name='Seymour Asses')
>>> print artist.all()
[{'name': 'Seymour Asses', 'id': 1}]

Bam. Easy as py.

Finding records

Time to do a bit of filtering.

>>> a = artist.filter(name='Seymour Asses')
>>> print a['id']
1

Pretty easy. Filter methods can be chained, as can limit()s and order_by()s. Observe:

>>> for i in range(15):
>>>   artist.create(name='Artist%s'%i)
>>> a = artist.all().limit(10).order_by('name', 'DESC')
>>> print len(a)
10
>>> print a[0]['name']
Artist15
>>> print a[-1]['name']
Artist6

See? What could be easier than that? Nothing, that's what. Moving right along...

Updating records

So, now we have a buttload of records in our DB. Next thing we may want to do is to change some stuff.

>>> a = artist.filter(id=1)[0]
>>> print a['name']
Seymour Asses
>>> a['name'] = 'Seymour the Petrified Dog'
>>> print a.save_required
True
>>> a.save()

Now you have updated and saved a record.

That covers the basics of Conrad. There's not much beyond that at this point, but soon I will be adding foreign key dereferencing, better validations, and more other things.

Clone this wiki locally