Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Using Databases in Python!
You have completed Using Databases in Python!
Preview
Now that we have a model, we need to be able to put information into it and then get that information back out. That's where queries come in.
New Terms
-
.create()- creates a new instance all at once -
.select()- finds records in a table -
.save()- updates an existing row in the database -
.get()- finds a single record in a table -
.delete_instance()- deletes a single record from the table -
.order_by()- specify how to sort the records -
if __name__ == '__main__'- a common pattern for making code only run when the script is run and not when it's imported -
db.close()- not a method we used, but often a good idea. Explicitly closes the connection to the database. -
.update()- also something we didn't use. Offers a way to update a record without.get()and.save(). Example:Student.update(points=student['points']).where(Student.username == student['username']).execute()
Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Select, let's us pick rows out of the
table to use.
0:00
Save, will update an existing row in the
database.
0:01
Get, will fetch a single record from the
database.
0:04
And finally, delete_instance will delete a
row from a table.
0:06
There are many other methods on Peewee
models but I leave them for
0:10
you to read about in the docs linked in
the teacher's notes.
0:13
Okay.
So let's setup some of the info that we
0:16
want to have inserted into our database.
0:19
Like I said, we're gonna use a dictionary
to hold on to all the data.
0:23
And the reason for
0:28
that is because a dictionary is easy to
look back to, it's easy to update.
0:29
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up