Hello,

First of all I'm new to DBs so please don't hang me on a tree )))


I have basic and simple questions(at least it simple for me ^_^ ).

While googling and reading some tutorials about ORM DBs at whole, I still 
cannot build up a logical picture of how it works. 

Here  is the brief example (I will use pony orm syntax cause it is readable 
and simple )))) )

Lets begin.

First of all we need ti initialize a DB, like this:

>>> db = Database('sqlite', '/path/to/the/test_db.sqlite', create_db=True)

This command says that we've created a DB file named "test_db.sqlite" and 
it based on sqlite.

Then, let's say  I'll create two classes(connected via cars attribute):

>>>class Person (db.Entity):
          name = Required(str)
          age = Required(int)
          cars = Set("Car")
...

>>>class Car(db.Entity):
             make = Required(str)
             model = Required(str)
             owner = Required(Person)
...

Finally I make a mapping between these classses and "test_db.sqlite"

>>>db.generate_mapping(create_tables=True)


So, at this moment I have some classes stored in the memory (cause I worked 
in python interactive shell) and physical "test_db.sqlite" placed on my 
hard drive.
This file is actually contains an empty tables, am I right? Because I 
didn't initialized any entities yet.
I can keep working via interactive console in order to accomplish this task 
and then just write "commit()" command in order to update "test_db.sqlite" 
file.

Till this point everything looks fine.(I hope)

The question arises when I ask my self how all this stuff should work with 
my code?

I mean that I need to write some functions that will update my_db data. 

Suppose  I have "my_main_routine.py" which, among the other things, imports 
some data from xls files. But how do I actually put these xls values in my 
data base?

Should it look like that:

my_main_routine.py
my_db_classes.py ---- which will consist of all the classes I've created 
before.(i.e. Person and Car)
"test_db.sqlite" ---- supose I've already mapped my classes to this file. 



How do I handle all my these db-classes inside of main_routine?

Does it look like that(generally):

# my_main_routine.py

import my_db_classes

# db binding commands:
db.bind('sqlite', '/path/to/the/test_db.sqlite', create_db=True)

......... my_code_ for_importing_xls_values.......
..........my_code_ for_importing_xls_values.......
..........my_code_ for_importing_xls_values.......
..........my_code_ for_importing_xls_values.......

# now I need to pass these xls values to my db
# so should it be written like that? -->

p1 = Person(name = 'John', age = 20)
p2 = Person(name = 'Mary' , age = 23)
c1 = Car(make='Toyota', model = 'Prius', owner=p2)
c2 = Car(make='Ford', model='Exploler', owner=p1)

#and then just

commit()

#is that all? I just work directly with classes (in SQLAlchemy for 
particularly) or I need some decorators/ other stuff?

EOF



Furthermore, as I can barely understand, in order to work with db inside 
my_routine file I need (preferably) create a separate files i.e.:

my_db_classes.py
my_db_initial_mapper.py
# in this file my db_mapping functions should be placed
# as a result the "my_db.sqlite" will be created.

and then my main code should look like this:

# my_main_routine.py
import my_db_classes
import my_db_initial_mapper

#binding functions may be placed in main_routine file(right?)

...... binding code.......... 

......some xls related code.........

......entities assignment code.....

commit()

EOF

Is this right? 


I think my problem appeared because of a lot of examples about databases 
are using interactive prompt so, it's kinda tricky to combine all of it 
when talking about python modules.


Hope my question is clear enough, in case it doesn't I'll may best to write 
it more clearly next time ))))

Any help will be highly appreciated. 


Ivan.

P.S. What about raw SQL code? Should I write it directly in 
my_main_routine.py  or there some special ways of implementation? 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to