Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Using SQLAlchemy
Lecture: Running the inserts
0:00 I think we're ready to try this.
0:01 Let's go ahead and run this and see what happens.
0:04 Off it goes, package name
0:06 let's go with SQLAlchemy to start.
0:10 Summary is ORM4 for Python.
0:13 Author is Mike Bayer.
0:16 License, let's just guess, it's MIT.
0:18 The first version is 123.
0:23 And it's that many bytes.
0:27 The second one is going to be 2.0.0
0:30 and it's a little bit bigger, like so.
0:34 So that inserted the first one.
0:35 Let's do one more and say Flask, Microframework for Python.
0:42 Let's go with Armand and David, right?
0:45 Armand, originally, David Lord these days.
0:48 Let's just say this is BSD.
0:50 I have no idea what it is.
0:51 And it's 1.0.0 and 1.0.02.
0:59 There we go, that one can be bigger than one byte.
1:00 All right, I think we're good.
1:01 Let's go ahead and stop this.
1:04 Notice, there were no crashes.
1:05 That's pretty killer already.
1:06 That's a good chance that something worked
1:08 But let's go look in the database.
1:10 So if I go over to Packages and I say
1:12 Jump to Console and say, select * From Packages.
1:18 Moment of truth.
1:20 Tada! There they are, how awesome?
1:22 We didn't set some of the values
1:24 but we did set many of them.
1:25 You can see everything we typed in there.
1:27 Pretty awesome, isn't it? What about releases?
1:33 Run those, look at that. There they are!
1:37 And you can see what package they come from
1:39 SQLAlchemy or Flask.
1:40 That's really cool and that's actually the relationship.
1:43 So I could go over here and, say, where
1:50 package ID equals SQLAlchemy? Is it 1?
1:56 I don't think it's what it equals.
1:58 Here we go. So, we can go do the query for that
2:00 and this is when we actually go back
2:02 and do queries with SQLAlchemy
2:03 and we touch that releases folder.
2:05 It's going to do, basically, this query
2:07 but it's also going to add an order by
2:10 major version descending.
2:12 And then minor and then whatever
2:13 but this should be enough. There we go.
2:16 So we'll get them back exactly in the order
2:18 you would want them.
2:20 All right, so this is how we insert data.
2:21 Super, super simple, right?
2:23 We go and just treat these more or less like objects.
2:25 We create them, we set their properties
2:27 we click them together through their relationships
2:31 and we add them to the session, create a session
2:33 and add it.