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
What good is a database without tables? Let's build a model, connect to our database, and turn our model into a table.
New Terms
- model - A code object that represents a database table
-
SqliteDatabase- The class from Peewee that lets us connect to an SQLite database -
Model- The Peewee class that we extend to make a model -
CharField- A Peewee field that holds onto characters. It's avarcharin SQL terms -
max_length- The maximum number of characters in aCharField -
IntegerField- A Peewee field that holds an integer -
default- A default value for the field if one isn't provided -
unique- Whether the value in the field can be repeated in the table -
.connect()- A database method that connects to the database -
.create_tables()- A database method to create the tables for the specified models. -
safe- Whether or not to throw errors if the table(s) you're attempting to create already exist
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
To demonstrate this, let's make a model
to represent a student here at Treehouse.
0:00
So, let's start by creating a new file
that we're going to call, Students.py.
0:03
And I'm going to hold all
of our information about
0:10
the different students in a dictionary.
0:13
But before I do that, I need to go
ahead and make the database and
0:15
the model and all of that stuff.
0:20
You know,
cart before the horse kind of thing.
0:22
So, to do most of this, I need to import
the stuff that I need from peewee.
0:26
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