Re: [elixir] How do you load a database?

18 views
Skip to first unread message

Gaetan de Menten

unread,
Nov 10, 2009, 4:57:50 AM11/10/09
to sqle...@googlegroups.com
On Tue, Nov 10, 2009 at 05:28, Kevin Ar18 <kevi...@hotmail.com> wrote:
> I know, this is a really basic question, but...
> Is there documentation or a tutorial on how to read from an
> existing database using Elixir?

> Is there any documentation that describes the differences between SQLAlchemy
> and Elixir and when you should use Elixir and which parts you should use
> the SQLAlchemy API for?

Not really. What many people don't understand is that Elixir only
provides a convenient way to define your mappers and tables. Nothing
more. So usage after the initial setup (inserts, querying, deletes,
... whatever is the same as with SQLAlchemy). Elixir also provides a
few shortcut for queries but they are entirely optional and you can
perfectly use exactly the same way as in SQLAlchemy.

> I've gone through the Elixir tutorial and looked over the SQLAlchemy
> documention - not to mention searching the API on both -- even looked at
> some of the Elixir sample code.  Eventually, I hit a wall (no more Elixir
> documentation that I could find).
>
> Essentially, everything I see tells me how to create a new database
> connection with new tables and new data, but not how to load a database long
> after it has been made.
>
> Of course, I could probably do this in no time using pure SQLAlchemy if I
> followed their docs, but I wanted to do it with Elixir, which is probably
> different, right?

What do you want to read from the database? The rows or the table
definition themselves? If you want to read the definition of the
tables from the database (for example, if you are working with a
legacy database that you cannot change), you can use the autoload
feature. See the API docs and Elixir tests for some explanation and
examples. If this is your case though, you might want to use
SQLAlchemy's built-in "declarative" extension, because in that
particular case Elixir has virtually no benefit over "declarative"
(because Elixir's speciality is to generate the tables for you).

If all you want to do is read the rows from an existing database,
well, you just define your entity as you did in the first part of your
code below and simply not call create_all().

> BTW, here is an example of what I am trying:
>
> from elixir import *
>
> metadata.bind = "sqlite:///db.db"
> class Planet(Entity):
>  date = Field(String)
>  cities = Field(Integer)
>
> setup_all()
> create_all()

You should only call that line (createall) when you want to create the tables.

> Planet(date=url_date, cities=planet_numbers)
> session.commit()

Hope it helps,
--
Gaëtan de Menten
http://openhex.org

Gaetan de Menten

unread,
Nov 11, 2009, 9:19:56 AM11/11/09
to sqle...@googlegroups.com
On Wed, Nov 11, 2009 at 03:03, Kevin Ar18 <kevi...@hotmail.com> wrote:
>> Hope it helps,
> Try as I may, I can't get it to work....  Let me show you the real code:
>
> planet_names = ["Janus","Planet2"]
>
> from elixir import *
> metadata.bind = "sqlite:///citiesxl.db"
> class Base_Planet(Entity):

>  date = Field(String)
>  cities = Field(Integer)
> for planet_name in planet_names:
>  exec(str(planet_name)+" = Base_Planet")
> setup_all()
> create_all()
> for i in range(len(planet_names)):
>  exec(str(planet_names[i])+"(date=url_date, cities=planet_numbers[i])")
> session.commit()
> print Janus.query.all()
>
> -----
> the key point is that I want to dynamically create the tables, meaning I
> can't hard-code the table name. So far, all of this code works.
> ----
> Now, when I try to view the data separately, it doesn't work:
>
> from elixir import *
> metadata.bind = "sqlite:///citiesxl.db"

You are missing the declaration of your classes here. What people
usually do is have that in a seperate module and simply import the
module at this point. If you don't do that, how is the program
supposed to know what is that Janus class that you refer to? There is
one way to do approximately what I think you want, you need to use the
Sqlsoup SQLAlchemy extension, instead of Elixir.

> setup_all()
> print metadata.sorted_tables
> print Janus.query.all()

Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages