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
                      Models in peewee are Python classes.
                      0:00
                    
                    
                      Each column in the table, each aspect
that we want to remember about our thing.
                      0:02
                    
                    
                      Is an attribute on the Model Class.
                      0:06
                    
                    
                      To demonstrate this, let's make a model
to represent a student here at Treehouse.
                      0:08
                    
                    
                      So, let's start by creating a new file
that we're going to call, Students.py.
                      0:12
                    
                    
                      And I'm going to hold all
of our information about
                      0:19
                    
                    
                      the different students in a dictionary.
                      0:22
                    
                    
                      But before I do that, I need to go
ahead and make the database and
                      0:24
                    
                    
                      the model and all of that stuff.
                      0:29
                    
                    
                      You know,
cart before the horse kind of thing.
                      0:31
                    
                    
                      So, to do most of this, I need to import
the stuff that I need from peewee.
                      0:35
                    
                    
                      But unlike most of the platform
that we've been writing,
                      0:40
                    
                    
                      we're going to be using a lot
of different stuff in peewee.
                      0:43
                    
                    
                      And so, the convention for
that is, to actually go ahead and
                      0:46
                    
                    
                      just import everything.
                      0:48
                    
                    
                      I don't really like doing this,
it's not my favorite technique.
                      0:52
                    
                    
                      But this is the convention for
working with peewee and
                      0:57
                    
                    
                      it's pretty important to
stick to conventions.
                      0:59
                    
                    
                      Because that way your code,
looks like other people's code.
                      1:01
                    
                    
                      Everybody can make assumptions and
just work.
                      1:05
                    
                    
                      So we're going to import everything.
                      1:08
                    
                    
                      So now, let's make our database.
                      1:09
                    
                    
                      We're going to be using SQLite so
we wanna make a SQLite connection.
                      1:12
                    
                    
                      Peewee will create the database for
                      1:17
                    
                    
                      us if it doesn't exist,
at least when we're using SQLite.
                      1:18
                    
                    
                      So, Sqlitedatabase, and
                      1:21
                    
                    
                      I'm going to call it Students.db.
                      1:25
                    
                    
                      The .db is kind of a bit of a convention,
because it says, hey, this is a database.
                      1:29
                    
                    
                      You don't have to, of course,
if you don't want to.
                      1:35
                    
                    
                      And you can name the file
anything you want.
                      1:38
                    
                    
                      If you don't want to call it students,
if you want to call it people, whatever.
                      1:39
                    
                    
                      Go for it.
                      1:44
                    
                    
                      But now, we need our model.
                      1:45
                    
                    
                      Models in peewee are classes, but
they inherit from the model class.
                      1:48
                    
                    
                      So let's do, class Student and
it inherits from Model.
                      1:53
                    
                    
                      And we always name our
models with a singular name.
                      1:58
                    
                    
                      Notice how I'm using Student and
not Students.
                      2:03
                    
                    
                      So, why is that?
                      2:06
                    
                    
                      Well, it's because a model
represents a single
                      2:07
                    
                    
                      item in the database, theoretically.
                      2:09
                    
                    
                      It wouldn't make sense then,
to give them a plural name.
                      2:12
                    
                    
                      You'll see this convention used
all over the place with peewee.
                      2:15
                    
                    
                      And you'll also see this
used everywhere in Django.
                      2:18
                    
                    
                      Now, our model needs some attributes.
                      2:22
                    
                    
                      These attributes will become the fields or
columns in the database table.
                      2:24
                    
                    
                      Let's just hold onto two things.
                      2:28
                    
                    
                      We're going to hold onto
the student's username.
                      2:29
                    
                    
                      CharField.
                      2:33
                    
                    
                      And let's hold onto their points.
                      2:40
                    
                    
                      So, what's up with these?
                      2:45
                    
                    
                      Well, a CharField is a field
that holds characters.
                      2:49
                    
                    
                      Somebody will say, CareField but
I always find that really hard to say.
                      2:53
                    
                    
                      In sequel terminology,
this is a VarChar field.
                      2:56
                    
                    
                      Because it has a variable length,
VarChar variable character.
                      3:00
                    
                    
                      We have to tell it the maximum number
of characters that we want to store.
                      3:05
                    
                    
                      So we're gonna set this to 255.
                      3:09
                    
                    
                      That should hold pretty much any username
that we're going to ever come across.
                      3:10
                    
                    
                      The unique attribute,
                      3:16
                    
                    
                      the second argument there, says that
we want the usernames to be unique.
                      3:17
                    
                    
                      We don't wanna have multiple records for
the same student.
                      3:23
                    
                    
                      Our points attribute which
is an integer field.
                      3:26
                    
                    
                      You probably guessed,
integer fields hold integers.
                      3:29
                    
                    
                      And what we've said with this
default keyword argument here,
                      3:33
                    
                    
                      is that if we don't supply a points.
                      3:37
                    
                    
                      Then it should just default it to zero.
                      3:40
                    
                    
                      Now, we didn't set a default
on the user name field.
                      3:43
                    
                    
                      So, if we don't provide a user
name we're going to get an error.
                      3:46
                    
                    
                      The last thing that we have to do is,
                      3:49
                    
                    
                      we have to tell the model
what database it belongs to.
                      3:50
                    
                    
                      And we do that, by defining a class
inside of our class that's called Meta.
                      3:54
                    
                    
                      Yeah, you can have classes
inside of classes.
                      4:00
                    
                    
                      Now, metaclasses are something
you're going to hear about
                      4:03
                    
                    
                      a lot of different places.
                      4:06
                    
                    
                      And they're a huge part of programming,
but this isn't a metaclass.
                      4:07
                    
                    
                      This is just a class that's called meta.
                      4:11
                    
                    
                      It's describing the class
that it belongs to.
                      4:13
                    
                    
                      The name has some meaning, but
                      4:15
                    
                    
                      it doesn't necessarily mean
the thing that the name reflects on.
                      4:17
                    
                    
                      I know that's a little weird, but again,
                      4:21
                    
                    
                      this is a convention you're going
to see in peewee and in Django.
                      4:23
                    
                    
                      So, we can use the metaclass
to define all sorts of things.
                      4:27
                    
                    
                      Like which fields should be indexed,
                      4:31
                    
                    
                      how things should be ordered by default,
all that stuff.
                      4:33
                    
                    
                      But for right now,
                      4:36
                    
                    
                      we're just going to stick to one attribute
which is the database attribute.
                      4:37
                    
                    
                      And we're going to set
database as being equal to db.
                      4:40
                    
                    
                      which is our SQLite database
we created up on line three.
                      4:44
                    
                    
                      Okay so, we have a database and
we have a model that's been described.
                      4:49
                    
                    
                      We should use them.
                      4:54
                    
                    
                      So, let's add some code in here.
                      4:55
                    
                    
                      That if our file is run directly and
not imported.
                      4:59
                    
                    
                      That it will go ahead and
connect to our database.
                      5:06
                    
                    
                      And it will create tables and the tables
we want it to create is, or are student.
                      5:10
                    
                    
                      We only have one table.
                      5:17
                    
                    
                      But something we want to do is,
going to be sure and say safe equals True.
                      5:19
                    
                    
                      The reason we have to pass in the safe
equals True keyword argument
                      5:23
                    
                    
                      is so, that if we run this multiple
times toward the database that's
                      5:29
                    
                    
                      already been created,
                      5:32
                    
                    
                      and the table has already been created,
peewee won't freak out and break.
                      5:32
                    
                    
                      It'll just go, okay,
cool, everything's there.
                      5:37
                    
                    
                      Let's move on.
                      5:40
                    
                    
                      Okay, let's try and run this.
                      5:42
                    
                    
                      We come down here, python students.py.
                      5:46
                    
                    
                      And then I should be able to run
this a second time with no problems.
                      5:49
                    
                    
                      Yay!
                      5:55
                    
                    
                      All right, so let's do an ls,
and there's our students.db.
                      5:55
                    
                    
                      We're not going to talk about
everything in SQLite.
                      5:59
                    
                    
                      But let's use SQL lite real quick, to look
inside there and see what's going on.
                      6:02
                    
                    
                      So, we can do sqlite3, students.db.
                      6:06
                    
                    
                      That'll connect to our database.
                      6:10
                    
                    
                      We can do .tables and
that will show us all of our tables.
                      6:12
                    
                    
                      So, we have a student table.
                      6:17
                    
                    
                      And we can do say, select * from student.
                      6:19
                    
                    
                      And we get back nothing,
because we haven't put anything in there.
                      6:24
                    
                    
                      And then we can do .exit, and
that'll go ahead and leave SQLite.
                      6:27
                    
                    
                      Great, we're now ready
to go build something.
                      6:31
                    
                    
                      Connecting to a database and
creating a model are both pretty simple.
                      6:35
                    
                    
                      Most of that is thanks to peewee's API.
                      6:38
                    
                    
                      Now that we have a table,
let's insert some data into it and
                      6:40
                    
                    
                      see about getting that data back out.
                      6:43
                    
              
        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