Ivan Evstegneev <bravo....@gmail.com> wrote:

> 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. 


I think you might like some of the videos I have up at
http://www.sqlalchemy.org/library.html#talks. The “Session in Depth” gets
pretty step-by-step about the internals.


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

heh…why aren’t you asking on *their* list then ? :)



> 
> 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?

um…well if create_tables is like create_all(), then yes. go to a shell and
type “sqlite3 <name_of_yourfile>”, you can query it directly.

> 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. 

if you’re writing a script to read an .xls file, I wouldn’t overthink it to
start. If you’re going to use multiple files, I’d advise using a traditional
“package” setup, e.g. with an __init__.py and all that. But an .xls reader
just to prototype probably can be in one file to start.

> 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)

that’s an easy way to do it, sure. There’s ways that people might want to
automate how the names get matched up but I think if you’re starting out, I
think on a name-by-name basis works, you’d have a loop that iterates through
rows in the XLS, and for each row it makes a bunch of objects.

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

that is all, if you put things in the session with session.add() first, then
you just commit and it flushes them.

> 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? 

that could work sure.


> 
> 
> 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.

You might want to poke around for example applications. I think most of them
are going to be web apps though which is likely more than you need here.


> 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? 

for putting .xls rows into a database with the ORM you wouldn’t need much
raw SQL. If you did, then yes you’d just have the strings for that SQL in
the file.

where you’re at, just do what works, and then when you have that, you can
iterate on it to make it better.


> -- 
> 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.

-- 
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