Changes between Version 20 and Version 21 of TutorialDivingIn
- Timestamp:
- 10/15/09 16:59:50 (4 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TutorialDivingIn
v20 v21 111 111 }}} 112 112 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: 113 This 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: 116 114 117 115 {{{ … … 121 119 }}} 122 120 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]121 Not 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] 128 126 >>> movie.year = 1983 129 >>> movie = Movie.query.all() 127 >>> session.commit() 128 >>> Movie.query.all() 130 129 [<Movie "Blade Runner" (1983)>] 131 130 }}} 132 131 133 Or even delete it: 134 135 {{{ 136 #!python 137 >>> Movie.delete(movie) 132 We can even delete it easily: 133 134 {{{ 135 #!python 136 >>> movie.delete() 137 >>> session.commit() 138 138 }}} 139 139 … … 148 148 Close the interpreter now and delete the database file (`movies.sqlite`)^4^, we will recreate and populate it in the next step. 149 149 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. 150 So 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. 153 151 154 152 ^3^ ''Make sure, you're running your interpreter from the directory where you saved the `model.py` file.'' [[BR]]
