#100DaysOfCode in Python Transcripts
Chapter: Days 91-93: Database access with SQLAlchemy
Lecture: Demo: Introducing our app
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's get right into writing some code. Now, if you look in the GitHub Repository, there's two sets of code here. They look exactly the same right now
0:11
but they're going to be very different at the end. In this project, we're going to start from an existing application.
0:17
One that's already done and all we're going to do is add database access to it. It's a bit of a trade off that happens to be
0:24
more realistic, more entertaining, but slightly more complex by starting with something instead of entirely from scratch.
0:30
But we'll isolate the SQLAlchemy pieces really well. So this one, this persistent RPS starter, this is going to stay in the starter state.
0:38
This one on the other hand, we're going to evolve to the final version. Now before I open this in PyCharm,
0:44
let me come over here and create a virtual environment. And now on MAC OS we can drop it here, or on Windows or Linux, you say file, open .directory.
0:55
So let's go through and have a quick look at what we got here. First, tell PyCharm to chill out on the virtual directory. Start up the program.
1:03
So this is the thing that we're going to run. Let's just go ahead and run it so you see what happens here, in fact, yeah just run it like this.
1:10
So we're going to play, you might've guessed from the RPS, Rock Paper Scissors. And we're going to use a database to store
1:16
all the players who have played the games, all the games that have been played, the roles, who has won, who has the highest score,
1:23
we'll do reporting on that so we'll sort of show the high score screen by just doing a database query and order by times they won, things like that.
1:32
It starts out asking what your name is. And then we're going to play not rock paper scissors, but we're going to play 15-way rock paper scissors.
1:40
So really fun, we have things like: the devil, and the dragon, and the sponge, and so on. So let's start by throwing standard rock.
1:49
I need that a little higher so we can see what's going on here. Oh! I've been defeated. I threw rock, but the computer threw water.
1:54
Apparently, water beats rock. How about fire? I'm defeated again, this is not going to be good. I'll throw a snake. The computer also threw the snake,
2:03
so let's throw eight. Paper. They threw tree. I'm not looking... I don't think it's going to matter what I'll throw so let's throw a tree.
2:15
I threw a tree, they threw a scissors, I win, but the computer wins 3-1. We had one tie, three wins for the computer, one for me. Therefore, I lose.
2:23
So we're going to take this game, as you can see it lets you play but it doesn't show you a high score. It has no history of the game.
2:29
If I run it again, it just entirely starts from scratch. So we're going to go over here and we're going to upgrade this by using SQLAlchemy
2:37
to make it remember.