Changes between Version 20 and Version 21 of TutorialDivingIn

Show
Ignore:
Timestamp:
10/15/09 16:59:50 (4 years ago)
Author:
ged (IP: 77.109.114.23)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TutorialDivingIn

    v20 v21  
    111111}}} 
    112112 
    113 This will tell SQLAlchemy to generate all of the SQL to insert the Movie into 
    114 the database, and then execute that SQL. Now, to see a list of all the movies  
    115 in our database, simply type: 
     113This will tell SQLAlchemy to generate all of the SQL to insert the Movie into the database, and then execute that SQL and finally commit the current transaction. Now, to see a list of all the movies in our database, simply type: 
    116114 
    117115{{{ 
     
    121119}}} 
    122120 
    123 Not many, but exactly what we expected. This can be used to access your objects: 
    124  
    125 {{{ 
    126 #!python 
    127 >>> movie = Movie.query.all()[0] 
     121Not many, but exactly what we expected. Now let us modify our test movie: 
     122 
     123{{{ 
     124#!python 
     125>>> movie = Movie.query.first() # .first() is equivalent to (but nicer than) .all()[0] 
    128126>>> movie.year = 1983 
    129 >>> movie = Movie.query.all() 
     127>>> session.commit() 
     128>>> Movie.query.all() 
    130129[<Movie "Blade Runner" (1983)>] 
    131130}}} 
    132131 
    133 Or even delete it: 
    134  
    135 {{{ 
    136 #!python 
    137 >>> Movie.delete(movie) 
     132We can even delete it easily: 
     133 
     134{{{ 
     135#!python 
     136>>> movie.delete() 
     137>>> session.commit() 
    138138}}} 
    139139 
     
    148148Close the interpreter now and delete the database file (`movies.sqlite`)^4^, we will recreate and populate it in the next step. 
    149149 
    150 So far you've seen how to declare simple entities, create objects, store them, modify them, or even delete them, 
    151 to the database and retrieve them again. Not too much magic, but a lot more  
    152 pleasant to the eye compared to calling lowlevel SQL-statements. 
     150So far you've seen how to declare simple entities, create objects, store them to the database and retrieve them again, modify them, or even delete them. Not too much magic, but a lot more pleasant to the eye compared to calling lowlevel SQL-statements. 
    153151 
    154152^3^ ''Make sure, you're running your interpreter from the directory where you saved the `model.py` file.'' [[BR]]