And ... Sorry for my bad english. 

Em 2013-04-18 20:23, Richard
Gerd Kuesters escreveu: 

> Hey Phillip! 
> 
> First, you must
understand a simple concept: when you declare a table in SA, either
declarative or traditionally, you're already sayin' to the SA engine
that "there are tables". Then, you can create them (if they do not
exist), populate them and drop'em. You can even specify your own SQL
code using SA's dialects. 
> 
> Let me explain it, well, with
pseudo-code (I'm sorry, I'm at home and I don't have [at least, recent]
SA code around here): 
> 
> -- 
> 
> class First(object): 
> 
> pass 
>

> class Second(object): 
> 
> pass 
> 
> my_first_table =
Table('first', metadata, ....) 
> 
> my_second_table = Table('second',
metadata, ...) 
> 
> mapper(First, my_first_table) 
> 
> mapper(Second,
my_second_table) 
> 
> ## 
> 
> # at this point, SA knows there is 2
tables, and they're mapped to 2 objects 
> 
> ## 
> 
> my_engine =
create_engine('my-adapter://...') # the connection engine, example:
"sqlite://", which is an in-memory instance (good for testing) 
> 
> if
'create' in sys.argv: # now, we want to specifically CREATE everything
we mapped 
> 
> metadata.create_all(my_engine) # in here, SA creates
tables, constraints; and so on. if you want something like "drop table
if exists ...", I think you must specify it in your dialect, use events
or, well, wait for someone here with more info about it :) I don't know
if SA checks for alter columns, except if you use its functions for it

> 
> # by now, all we do is fun! 
> 
> # creating objects 
> 
>
first_1st = First() 
> 
> second_1st = First() 
> 
>
session.add([first_1st, second_1st]) # in here, you put your mapped
objects in your session object 
> 
> session.commit() # now, they are
turn into INSERT statements (99,999% of the time, lol) 
> 
> # by now,
the first table is populated 
> 
> first_2nd = Second() 
> 
> second_2nd
= Second() 
> 
> # but the second table nope (we need to tell the
engine's session that we want it to do it, right?) 
> 
> # so, what to
expect? 
> 
> assert(len(session.query(First).all()), 2) 
> 
>
assert(len(session.query(Second).all()), 0) 
> 
>
session.add([first_2nd, second_2nd, Second()]) 
> 
> # they are in
session! are they in the database? "yes", you might say, with you do not
want rollbacks and "autocommit" is True 
> 
>
assert(len(session.query(Second).all()), 0) 
> 
> session.commit() 
> 
>
# now what do you have? 2 rows in the "first" table 
> 
> print
session.query(First).all() 
> 
> # and 3 rows in the "second" table 
>

> print session.query(Second).all() 
> 
> #checking it 
> 
>
assert(len(session.query(First).all()), 2) 
> 
>
assert(len(session.query(Second).all()), 3) 
> 
> # ok, now I want to
drop them all 
> 
> if 'drop' in sys.argv: 
> 
>
metadata.drop_all(engine) # do not forget: metadata have *all* your SA
tables [and so on] in it 
> 
> print 'done!" 
> 
> -- 
> 
> Please, if
this is not what you're asking (and if you're already know that), let me
know a little specifically your problem. I like to help, and we're all
students :D 
> 
> Kind regards, 
> 
> Richard. 
> 
> Em 2013-04-18
19:30, Philipp Kraus escreveu: 
> 
>> Hi Gerd, 
>> I have taken a look
to the examples, but I don't understand, in which way I can use the
transaction with the create_all option. A short code excerpt: 
>> 
>>
engine = sqlalchemy.create_engine(env["connection"], echo =
env.get("DATABASE_VERBOSE", False) ) 
>> 
>> metadata =
sqlalchemy.MetaData() 
>> 
>> connect = engine.connect() 
>> 
>>
transaction = connect.begin() 
>> 
>> for i in mytables : 
>> 
>>
sqlalchemy.Table( i, metadata, *tablestructure ) 
>>
transaction.commit() 
>> connection.close() 
>> IMHO I must push the
transaction object to the Table call, but how can I do this? 
>> Thx 
>>
Phil 
>> 
>> Am Donnerstag, 18. April 2013 23:23:45 UTC+2 schrieb
Richard Gerd Kuesters: 
>> 
>>> Hello Phillip :)
>>> 
>>> IMHO, your
best options are the SA examples (in the source code) folder and, for
further abstractions, you could check SA test units :) 
>>> 
>>> Works
great for me!
>> 
>> -- 
>> 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
[email protected].
>> To post to this group, send
email to [email protected].
>> Visit this group at
http://groups.google.com/group/sqlalchemy?hl=en [1].
>> For more
options, visit https://groups.google.com/groups/opt_out [2].



Links:
------
[1] http://groups.google.com/group/sqlalchemy?hl=en
[2]
https://groups.google.com/groups/opt_out

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to