1 00:00:00,000 --> 00:00:09,399 [MUSIC] 2 00:00:09,399 --> 00:00:13,850 Hi there, my name is Megan and I'm a teacher here at Treehouse. 3 00:00:13,850 --> 00:00:16,790 My pronouns are she, her, hers. 4 00:00:16,790 --> 00:00:20,210 In this course, I'm excited to show you SQLAlchemy. 5 00:00:21,470 --> 00:00:27,360 SQLAlchemy is an O-R-M, or object-relational mapper. 6 00:00:27,360 --> 00:00:31,837 It's a Python code library that transfers data stored in a SQL 7 00:00:31,837 --> 00:00:35,010 database into Python objects. 8 00:00:35,010 --> 00:00:39,043 You can then use Python code to create, read, update, and 9 00:00:39,043 --> 00:00:43,560 delete data instead of writing straight SQL, which helps speed up 10 00:00:43,560 --> 00:00:49,120 development time, especially for developers who are not fluent in SQL. 11 00:00:49,120 --> 00:00:54,155 And it can often make managing data less error prone. 12 00:00:54,155 --> 00:00:58,200 SQL stands for Structured Query Language. 13 00:00:58,200 --> 00:01:02,340 It's a language for interacting with relational databases. 14 00:01:02,340 --> 00:01:07,310 Relational databases are structured like a traditional table with columns and rows. 15 00:01:08,430 --> 00:01:10,444 If you're not familiar with SQL, 16 00:01:10,444 --> 00:01:15,230 I highly suggest taking a peek at the prerequisites for this course. 17 00:01:15,230 --> 00:01:18,050 I've also listed them in the teacher's notes below. 18 00:01:19,410 --> 00:01:24,195 This prior knowledge will bridge the gap between working with the database using 19 00:01:24,195 --> 00:01:28,150 SQL, and working with the database using Python's SQLAlchemy. 20 00:01:29,490 --> 00:01:32,760 If you would like to follow along in workspaces, then go ahead and 21 00:01:32,760 --> 00:01:35,870 open up the workspace attached to this video. 22 00:01:35,870 --> 00:01:41,941 In the console, run pip install sqlalchemy, 23 00:01:41,941 --> 00:01:48,330 hit Enter, let it download and you're all set. 24 00:01:49,530 --> 00:01:54,057 If you would like to follow along locally, create a folder for your project and 25 00:01:54,057 --> 00:01:56,470 open it in your IDE of choice. 26 00:01:56,470 --> 00:01:58,466 I'm going to use Visual Studio. 27 00:02:04,690 --> 00:02:08,692 Let me go ahead and create our models.py file. 28 00:02:08,692 --> 00:02:13,003 We'll need to download SQLAlchemy, so we can use it. 29 00:02:13,003 --> 00:02:15,481 But first, let's create a virtual environment. 30 00:02:21,904 --> 00:02:28,152 Create a virtual environment by running python if you're on Windows, 31 00:02:28,152 --> 00:02:33,058 and python3 if you're on a Mac like I am, -m venv env. 32 00:02:33,058 --> 00:02:37,515 And this will start your virtual environment. 33 00:02:39,983 --> 00:02:42,203 You can see it's asking if I want to use it, I do. 34 00:02:42,203 --> 00:02:44,857 And you see our folder up here was created, and 35 00:02:44,857 --> 00:02:49,094 VS Code also adds its own little thing, you don't need to worry about it. 36 00:02:52,281 --> 00:02:55,229 Okay, now that we have our virtual environment created, 37 00:02:55,229 --> 00:02:56,830 we need to activate it. 38 00:02:56,830 --> 00:03:020,000 Now, if you're on Windows, you need to write and 39 00:03:00,000 --> 00:03:02,750 I need to make sure I get my slashes correctly, 40 00:03:02,750 --> 00:03:11,590 .\env\Scripts\activate. 41 00:03:11,590 --> 00:03:13,010 If you're on Windows. 42 00:03:13,010 --> 00:03:18,201 If you're on Mac, then you need to write it 43 00:03:18,201 --> 00:03:24,610 as source ./env/bin/activate. 44 00:03:24,610 --> 00:03:27,820 And this is all in the teacher's notes below as well. 45 00:03:27,820 --> 00:03:30,530 So you can reference it at any time. 46 00:03:30,530 --> 00:03:34,310 So go ahead and type that out, whichever one you need, hit Enter. 47 00:03:34,310 --> 00:03:36,785 And now you can see I have this env here, 48 00:03:36,785 --> 00:03:40,010 because I am now inside of my virtual environment. 49 00:03:41,180 --> 00:03:43,970 Perfect, now we can install SQLAlchemy. 50 00:03:43,970 --> 00:03:48,904 Same install, pip install sqlalchemy. 51 00:03:51,872 --> 00:03:56,479 Awesome, and then we can run pip freeze, caret or 52 00:03:56,479 --> 00:04:00,600 arrow to the right, requirements.txt. 53 00:04:00,600 --> 00:04:04,390 This is the file that will be important for others. 54 00:04:04,390 --> 00:04:07,128 And if I run it, it will create this file. 55 00:04:07,128 --> 00:04:11,770 And if I click into it, it'll tell me sqlalchemy has been installed. 56 00:04:11,770 --> 00:04:15,033 And this is the version that I'm currently using. 57 00:04:17,360 --> 00:04:20,580 Our requirements file is ready. 58 00:04:20,580 --> 00:04:23,086 If you want to create a repo for this project and 59 00:04:23,086 --> 00:04:28,460 others end up wanting to run your project to check it out, they will need this file. 60 00:04:28,460 --> 00:04:34,050 If you do wanna post this on GitHub, then let's also create a gitignore file. 61 00:04:34,050 --> 00:04:38,330 New File > .gitignore. 62 00:04:38,330 --> 00:04:40,720 Inside, we're going to write env. 63 00:04:40,720 --> 00:04:44,870 This will make sure that environment folder does not get shared to your repo. 64 00:04:44,870 --> 00:04:46,260 You don't want that to go up. 65 00:04:47,330 --> 00:04:50,017 We can also do this, .vscode. 66 00:04:50,017 --> 00:04:54,129 If you're like me and you're working in vs code, 67 00:04:54,129 --> 00:04:56,941 we don't need that to go up either. 68 00:04:56,941 --> 00:04:58,700 That's just for us locally here. 69 00:05:00,320 --> 00:05:06,759 There's also this .DS_Store on Macs that sometimes gets pushed up. 70 00:05:06,759 --> 00:05:11,760 You don't want those to go either, so you can add that to your gitignore file. 71 00:05:11,760 --> 00:05:13,010 And that should be good for right now. 72 00:05:13,010 --> 00:05:15,900 The most important one is your environment folder. 73 00:05:15,900 --> 00:05:20,586 This has a whole bunch of downloads in it, and anyone running your project only 74 00:05:20,586 --> 00:05:25,650 needs the requirements file to download those requirements themselves. 75 00:05:25,650 --> 00:05:29,363 So we don't wanna send them all of our files, they can download it for 76 00:05:29,363 --> 00:05:30,140 themselves. 77 00:05:32,220 --> 00:05:33,590 Okay, cool. 78 00:05:33,590 --> 00:05:38,118 Now that you have either workspaces or your local environment set up, 79 00:05:38,118 --> 00:05:41,120 let's check our version in the Python shell. 80 00:05:42,210 --> 00:05:43,111 I'm gonna run a clear. 81 00:05:43,111 --> 00:05:49,474 Then I'm gonna go python3 to make sure I'm in the newer version of Python. 82 00:05:49,474 --> 00:05:56,219 And then I'm gonna import at the top sqlalchemy, awesome. 83 00:05:56,219 --> 00:06:01,914 And then I'm going to run sqlalchemy.__version__. 84 00:06:01,914 --> 00:06:07,364 And awesome, it's the same version that's in our requirements file, 85 00:06:07,364 --> 00:06:12,840 but this is another way that you can check your version number. 86 00:06:12,840 --> 00:06:16,169 You can see I'm on version 1.3.22. 87 00:06:16,169 --> 00:06:19,789 And your version may be different and that's okay, 88 00:06:19,789 --> 00:06:23,170 technologies change quite frequently. 89 00:06:23,170 --> 00:06:25,397 So if you're on a newer version than I am, 90 00:06:25,397 --> 00:06:29,665 make sure to check the teacher's notes throughout the course. 91 00:06:29,665 --> 00:06:34,645 This way, you get the most up-to-date information to go along with your version. 92 00:06:34,645 --> 00:06:36,635 We're all set, time to dive in.