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 SQLAlchemy Basics!
      
    
You have completed SQLAlchemy Basics!
Preview
    
      
  Create a model for your book database using a CSV of books as your template. Import your model into your main app file.
This video doesn't have any notes.
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
                      It's time to create our database.
                      0:00
                    
                    
                      Let's start with our imports at the top.
                      0:02
                    
                    
                      From sqlalchemy import, and
                      0:09
                    
                    
                      we're going to do create_engine,
                      0:13
                    
                    
                      Column, Integer, String, and
                      0:18
                    
                    
                      Date cuz we'll need that for
the published date column.
                      0:22
                    
                    
                      And since this is getting a little long,
I'm gonna put these in parentheses so
                      0:30
                    
                    
                      then I can split them
onto multiple lines here.
                      0:36
                    
                    
                      There we go.
Okay, now we need to go
                      0:39
                    
                    
                      from sqlalchemy.ext.declarative
                      0:43
                    
                    
                      import declarative_base.
                      0:49
                    
                    
                      And then from sqlalchemy.orm
                      0:54
                    
                    
                      import sessionmaker.
                      0:59
                    
                    
                      Awesome, now we can create our engine
with a database named books.db.
                      1:03
                    
                    
                      Engine = create_engine, and
                      1:12
                    
                    
                      we'll need sqlite:///books.db,
                      1:16
                    
                    
                      and then I'm gonna set echo to false.
                      1:22
                    
                    
                      Once again, if you want echo to be true so
                      1:27
                    
                    
                      that you have those extra messages in
the console, that is totally up to you.
                      1:30
                    
                    
                      After our engine, let's do our session.
                      1:35
                    
                    
                      We need two session variables,
first one with a capital S.
                      1:38
                    
                    
                      And that's going to be
equal to sessionmaker, and
                      1:42
                    
                    
                      we're going to bind it to our engine,
which is our database.
                      1:45
                    
                    
                      And then we need to do session, which is
going to call Session with a capital S.
                      1:50
                    
                    
                      And then the last one we need
is Base with a capital B.
                      1:56
                    
                    
                      And that's going to be equal
to our declarative_base.
                      2:00
                    
                    
                      Next up goes our model,
let's add a couple spaces.
                      2:03
                    
                    
                      And let's do class, and let's call
it Book, and let's pass in our Base.
                      2:09
                    
                    
                      And then we'll need a table name,
                      2:16
                    
                    
                      __tablename__ =, and let's call it books.
                      2:21
                    
                    
                      And now we're on to our columns.
                      2:28
                    
                    
                      The first column will be our id.
                      2:31
                    
                    
                      So each book will have a unique
id value attached to it.
                      2:33
                    
                    
                      It's going to be an integer column, and
                      2:37
                    
                    
                      it's going to be our primary key,
primary_key = True.
                      2:40
                    
                    
                      Awesome, the next column will be for
the book title.
                      2:46
                    
                    
                      So title = Column, and
I'm going to do a string that says Title.
                      2:50
                    
                    
                      And then this column is
going to be all strings.
                      2:58
                    
                    
                      You're probably wondering why I
passed in a string to this column.
                      3:02
                    
                    
                      This string is how the name of this
column will look in the database.
                      3:07
                    
                    
                      Instead of title,
it'll be this Title with a capital T.
                      3:11
                    
                    
                      This isn't required, but it can make your
columns look a bit neater, in my opinion.
                      3:18
                    
                    
                      Next step will be author,
and that'll be a Column, and
                      3:25
                    
                    
                      I'll do Author as title, and
it will also be a String.
                      3:30
                    
                    
                      And then published date,
published_date = Column,
                      3:35
                    
                    
                      and I'm just gonna call this Published,
                      3:42
                    
                    
                      and this is going to be a Date.
                      3:47
                    
                    
                      And the last one is going to be price,
                      3:53
                    
                    
                      it's gonna be equal to Column,
which is going to be an Integer.
                      3:55
                    
                    
                      And oops, I forgot,
I also want to call it Price, there we go.
                      4:01
                    
                    
                      And don't forget to save, great.
                      4:09
                    
                    
                      Let's also add that dunder repr method,
                      4:12
                    
                    
                      def __repr__,
it will take self because this is a class.
                      4:18
                    
                    
                      And then it's going to
return a formatted string
                      4:24
                    
                    
                      that's going to have our
book information in it.
                      4:30
                    
                    
                      So let's do Title, and then self.title.
                      4:36
                    
                    
                      And then we're going to need the author,
                      4:43
                    
                    
                      that's going to be self.author.
                      4:48
                    
                    
                      Published with
                      4:53
                    
                    
                      self.published_date.
                      4:58
                    
                    
                      And then we need Price with
                      5:05
                    
                    
                      self.price, and save.
                      5:10
                    
                    
                      Perfect, over in app.py,
let's import our models.
                      5:15
                    
                    
                      And instead of just running import models,
                      5:22
                    
                    
                      we're going to import
the specific pieces we need.
                      5:25
                    
                    
                      So from models import and
I'm gonna put these in
                      5:30
                    
                    
                      parentheses cuz we're gonna
have a couple of them.
                      5:34
                    
                    
                      We're going to need Base and session, and
                      5:39
                    
                    
                      our Book class, and engine.
                      5:43
                    
                    
                      Then I'm gonna split these
into two lines here.
                      5:47
                    
                    
                      Okay, at the bottom,
let's add a few spaces.
                      5:53
                    
                    
                      And let's put in our dunder name.
                      5:57
                    
                    
                      __name__ = '__main__'.
                      6:01
                    
                    
                      Okay, inside of here, we can create our
                      6:06
                    
                    
                      database with Base.metadata.create_all,
                      6:11
                    
                    
                      and pass in engine, save.
                      6:17
                    
                    
                      Sweet, our model and
database are all ready to go.
                      6:23
                    
                    
                      You can run Python or python3 app.py, and
                      6:27
                    
                    
                      there's our database,
except I spelled it wrong.
                      6:31
                    
                    
                      I did boks, I guess,
[LAUGH] instead of books.
                      6:36
                    
                    
                      So if you're like me and you made that
mistake, you can fix your spelling,
                      6:39
                    
                    
                      save the file, and
then you can just delete this file.
                      6:44
                    
                    
                      Move to Trash, and then run python3 app.py
again, and it'll recreate your database.
                      6:52
                    
                    
                      There we go, books.db, that looks better.
                      6:56
                    
                    
                      Now that we've added all of this code,
it's time to add it to our repo.
                      6:58
                    
                    
                      So git add, and
then everything that we've done so far.
                      7:06
                    
                    
                      And then we're going to do git commit -m,
and
                      7:10
                    
                    
                      let's say we added our database model.
                      7:17
                    
                    
                      Hit Enter, and you can see, it shows
we have three files that have changed.
                      7:22
                    
                    
                      models.py, we had some rewrites to it,
and we created books.db.
                      7:29
                    
                    
                      And we also had some revisions to app.py.
                      7:36
                    
                    
                      And then we need to push it up
with git push origin master.
                      7:39
                    
                    
                      Perfect, let's click on our GitHub again,
refresh.
                      7:46
                    
                    
                      You can see our books.db is now here.
                      7:51
                    
                    
                      And if I click into our models.py,
there is our model we created.
                      7:54
                    
                    
                      Amazing work, Pythonistas.
                      8:01
                    
              
        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