SQLite Databases With Python - Full Course
Summary
TLDRIn the introduction video, instructor John Elder provides an overview of what to expect from his coding course offered on codemy.com. He emphasizes the need to install specific tools, namely Python, a text editor (Sublime Text), and a terminal (Git Bash). He assures that these tools are accessible for both Windows, Mac, and Linux users. Elder also explains the importance of ensuring Python is added to the system PATH during installation, and offers a coupon for course discounts. He concludes with directions for students to reach him for support and mentions additional resources available through his website.
Takeaways
- 👨🏫 Instructor: John Elder from codemy.com.
- 🛠 Course Tools: Python, Sublime Text, Git Bash.
- 💻 Compatibility: Available for Windows, Mac, and Linux.
- 🔄 Python Install Tip: Add to PATH to run anywhere.
- 💡 Discount: Use 'camp' for 65% off courses.
- 📞 Contact: Via codemy.com or video comments.
- 🔍 Access: Dozens of courses, hundreds of videos on codemy.com.
- 🔧 Free Tools: Python, Sublime Text, and Git Bash are all free.
- 🌎 Time Zone: Pacific Time for instructor's availability.
- 📑 Course Outline: Starts with setting up tools and contact info.
Timeline
- 00:00:00 - 00:05:00
John Elder, the instructor from Codemy.com, introduces the course. He outlines what to expect, the tools needed, and how to contact him. He encourages viewers to like the video and offers a discount for his courses. He mentions that he will be using a Windows computer but provides equivalents for Mac and Linux users.
- 00:05:00 - 00:10:00
John discusses the tools required for the course, including Python, a text editor like Sublime Text, and a terminal such as Git Bash for Windows users. He explains that SQLite comes with Python, so there's no need to download it separately. He also talks about installing Python and checking the 'add to path' option during installation.
- 00:10:00 - 00:15:00
John provides instructions on installing the Sublime Text editor and the Git Bash terminal. He guides users on downloading these tools and explains the installation process, ensuring they're set up to follow along with the course on any operating system.
- 00:15:00 - 00:20:00
The video emphasizes the simplicity of using SQLite with Python as it's already bundled with it. John demonstrates how to create a Python file, import SQLite3, and establish a connection to a new database file, showcasing how SQLite creates databases if they do not already exist.
- 00:20:00 - 00:25:00
John explains how to create a table inside the SQLite database and introduces data types used in SQLite. He uses a docstring method for organizing SQL commands and highlights the importance of committing statements after making changes in the database.
- 00:25:00 - 00:30:00
John moves on to inserting data into the SQLite table. He shows how to commit the command to save changes permanently and demonstrates the process of inserting multiple records at once using the `executemany` function with a list of tuples representing each record.
- 00:30:00 - 00:35:00
John teaches how to query the SQLite database to retrieve and display data. He explains using fetchall(), fetchone(), and fetchmany() to get data, and shares how to iterate through the results in Python. He also discusses formatting results for better readability with Python's string operations.
- 00:35:00 - 00:40:00
The concept of a primary key in databases is introduced, which helps in uniquely identifying database records. John shows how SQLite auto-generates a row ID for each record and explains its significance in database operations.
- 00:40:00 - 00:45:00
John covers how to filter query results using the WHERE clause and logical operators like AND and OR. He demonstrates pattern matching in SQL with the LIKE operator to search for partial matches within the database records.
- 00:45:00 - 00:50:00
John explains updating records in the SQLite database and emphasizes using row IDs to ensure only specific records get updated. He shows the potential issues of using non-unique identifiers when updating records and stresses best practices.
- 00:50:00 - 00:55:00
Similar to updating records, John goes through the process of deleting records using SQL DELETE statements. He reiterates the necessity of using unique identifiers like row IDs to avoid deleting unintended records in the database.
- 00:55:00 - 01:00:00
John discusses using the ORDER BY clause to sort query results in ascending or descending order. He explains how combining ORDER BY with LIMIT can restrict the number of results returned by a SQL query, helpful for large datasets.
- 01:00:00 - 01:05:00
Here, John demonstrates deleting an entire table from the database using the DROP TABLE command. He cautions that this operation is irreversible and the table will be permanently removed from the database.
- 01:05:00 - 01:10:00
John begins to convert the previous examples into reusable code structures, creating functions in Python. He sets up a new Python script to manage database operations, abstracting away individual commands into functions that can be reused.
- 01:10:00 - 01:15:00
John continues building the app by creating functions for adding records to the database. He writes a function to insert single records and tests it by calling the function from the managing script, ensuring it properly interacts with the database.
- 01:15:00 - 01:29:37
The final steps involve creating additional utility functions for deleting and adding many records. John encapsulates these operations into functions and tests them to verify that the interactions with the database work correctly. He discusses the benefits of organizing code in this modular fashion.
Mind Map
Video Q&A
What tools do I need for this course?
You will need Python, a text editor like Sublime Text, and a terminal like Git Bash. These tools are free.
Can I use a Mac or Linux instead of Windows?
Yes, you can use a Mac or Linux. The tools have equivalents on these operating systems.
How can I contact the instructor?
You can contact John Elder via his website codemy.com using the contact form, or by commenting on the video.
Is there a discount for codemy.com courses?
Yes, you can use the coupon code 'camp' to get 65% off a membership.
What version of Python should I download?
Download any version below Python 4 from python.org. At the time of the video, the recommended version was 3.7.3.
Is Python installation different on a Mac?
Yes, instructions are not provided in this video for Mac, but you can easily find guidance online.
Why should I reinstall Python even if I already have it?
Reinstalling ensures that you add Python to your Windows PATH, which is necessary for running Python anywhere.
What text editor will be used in the course?
John uses Sublime Text, but you can use any text editor you are comfortable with.
What is Git Bash, and why use it?
Git Bash is a terminal program that allows for version control commands. It's recommended over Windows' default terminals.
View more video summaries
How Is Your Phone Changing You?
HKT LEADERSHIP AND POSITIVE ORGANIZATIONAL BEHAVIOR PART 1
HKT LEADERSHIP & POSITIVE ORGANIZATIONAL BEHAVIOR PART 2
Everyone Was Lying About Metro - Balen Shah Explains Metro and Monorail
The New Geopolitics of Oil.
Typical vs Atypical Child Development - See Children in Action
- 00:00:00all right welcome to the course my name
- 00:00:01is john elder from cody.com and i'll be
- 00:00:03your instructor today in this video i
- 00:00:05want to spend just a couple of minutes
- 00:00:06sort of touching base talking a little
- 00:00:08bit about what to expect from the course
- 00:00:10talking about some of the tools that
- 00:00:11we're going to need and letting you know
- 00:00:13how to get in contact with me if you
- 00:00:14have any questions along the way but
- 00:00:16before we get started if you like this
- 00:00:17video you want to see more like it be
- 00:00:19sure to smash the like button below
- 00:00:20subscribe to that channel give me a
- 00:00:21thumbs up for the youtube algorithm and
- 00:00:23check out my website codeme.com where i
- 00:00:25have dozens of courses with hundreds of
- 00:00:26videos that teach you to code use coupon
- 00:00:28code camp to get 65 off membership
- 00:00:30that's all my courses videos and books
- 00:00:32for one-time fee at just 27 which is
- 00:00:34insanely cheap so right off the bat i'm
- 00:00:36on a windows computer hopefully you are
- 00:00:38too if you're on mac or linux though no
- 00:00:40big deal whatsoever you can definitely
- 00:00:41follow along all the tools and things
- 00:00:43that we're going to use in this course
- 00:00:44have a mac or linux equivalent and there
- 00:00:47shouldn't be any real changes at all
- 00:00:49between what i'm doing here and what
- 00:00:50you'll do on your mac or linux
- 00:00:52the code is going to be the exact same
- 00:00:54there's no changes that need to be made
- 00:00:56or anything like that it should work
- 00:00:57just fine so speaking of the tools that
- 00:00:59we're going to need we're going to need
- 00:01:00obviously python now sqlite actually
- 00:01:03comes with python so there's nothing
- 00:01:05else to download besides python it's
- 00:01:07we'll also need some sort of text editor
- 00:01:09to write our code on now i'm going to
- 00:01:10use something called sublime text if you
- 00:01:12have some other text editor that you
- 00:01:14like using pycharm visual studio code
- 00:01:17anything at all notepad plus plus
- 00:01:20absolutely feel free to use that if
- 00:01:21you're comfortable using that if you
- 00:01:22know how to use that if you know if you
- 00:01:25like using a particular one over another
- 00:01:27one absolutely feel free to do that no
- 00:01:29big deal whatsoever we also need some
- 00:01:31sort of terminal to write our commands
- 00:01:33on now windows computers come with
- 00:01:34several terminals there's the command
- 00:01:35prompt there's powershell now those
- 00:01:38aren't that great so i'm going to
- 00:01:39download something else something called
- 00:01:41the get bash terminal it's completely
- 00:01:42free sublime text is free too as well as
- 00:01:45python so nothing to buy in order to
- 00:01:47watch this course
- 00:01:49but like i said get bash works great so
- 00:01:50we're going to use that if you're on mac
- 00:01:52or linux you could just use the terminal
- 00:01:54that comes with mac or linux just go to
- 00:01:55the little search function type in
- 00:01:57terminal and you should be fine
- 00:01:59you can absolutely download the git bash
- 00:02:01terminal on mac or linux if you want to
- 00:02:03follow along exactly but it's definitely
- 00:02:05not necessary so
- 00:02:07that's really all the tools we're going
- 00:02:08to need in order to take this course
- 00:02:10like i said not much to sqlite as far as
- 00:02:13tools and stuff
- 00:02:15since it just comes with python so
- 00:02:16that's really cool so finally i want to
- 00:02:18talk about how to get in contact me if
- 00:02:20you have questions so i'm in las vegas
- 00:02:22which is pacific west coast time in the
- 00:02:24u.s so if you're on the other side of
- 00:02:26the world and it's during the day i'm
- 00:02:28probably sleeping back in vegas so just
- 00:02:29sort of keep that in mind when you sort
- 00:02:31of factor in response time but
- 00:02:33absolutely feel free to ask questions
- 00:02:35along the way if you don't understand
- 00:02:36something if your code's not working
- 00:02:38correctly if you just don't quite get
- 00:02:40something that i'm saying and you need
- 00:02:42some clarification shoot me a message
- 00:02:44that's what i'm here for i do this full
- 00:02:46time this is my job so i'm either
- 00:02:48creating courses or answering questions
- 00:02:50for students and so that's cool you can
- 00:02:52get in contact with me on my website
- 00:02:54kodamy.com there's a contact form you
- 00:02:56just click on it it shoots straight to
- 00:02:58my email so i'll see that as soon as i
- 00:03:00you know as soon as my email dings and i
- 00:03:02can see it you can also obviously post a
- 00:03:05comment below this video if you're
- 00:03:07watching this on youtube or wherever
- 00:03:09and that will work just fine too so
- 00:03:13there are no stupid questions if you
- 00:03:14know if you want to ask a question on
- 00:03:16something else i'm not as receptive but
- 00:03:19absolutely anything in this course
- 00:03:21anything in these videos you don't quite
- 00:03:22understand or like i said if your code
- 00:03:24isn't working if you're getting errors
- 00:03:25or something shoot me a message we'll
- 00:03:27get to the bottom of it and that's cool
- 00:03:33all right first things first we need
- 00:03:34python so chances are you might already
- 00:03:37have python on your computer even if you
- 00:03:39do i recommend you uninstall it
- 00:03:42reinstall it and download it again and
- 00:03:44reinstall it and i'll show you why in
- 00:03:46just a second so to do that
- 00:03:48we're going to head over to
- 00:03:50python.org you could google it if you
- 00:03:52want or just type in python.org and come
- 00:03:54to downloads and you can see right here
- 00:03:56says
- 00:03:57python3.7.3 that's the most current
- 00:03:59version right now if that version has
- 00:04:01changed by the time you watch this video
- 00:04:03as long as it's not version 4 anything
- 00:04:05below 4 go ahead and download whatever
- 00:04:07version it is perfectly fine so i'm
- 00:04:10gonna go ahead and click this button
- 00:04:12and we want to save this anywhere i'm
- 00:04:13just going to save it on my desktop
- 00:04:15and it's not a very big file in fact
- 00:04:18it's boom it already downloaded and so
- 00:04:21go ahead and click it
- 00:04:22to install this thing
- 00:04:24and we want to run it
- 00:04:27oops clicked it a couple of times there
- 00:04:29okay so
- 00:04:31install python 3.72 32-bit now this is
- 00:04:33the first thing there is a 64-bit
- 00:04:35version the 32-bit version is actually
- 00:04:38the one that you want it's the most
- 00:04:39common one uh it works perfectly fine on
- 00:04:4164-bit computers we all have a 64-bit
- 00:04:44computer so uh
- 00:04:46don't try and go find the 64-bit you can
- 00:04:49download the 64-bit version from the
- 00:04:51website i don't recommend that you do
- 00:04:52that it's just a hassle there's no real
- 00:04:55benefit to doing that
- 00:04:56so this is the one we want now before we
- 00:04:58click this button this is the most
- 00:05:00important thing right here
- 00:05:02and
- 00:05:03it is so very important and this is why
- 00:05:05i recommend that you download this and
- 00:05:07reinstall it even if python is already
- 00:05:09on your computer
- 00:05:10and that's this little check box right
- 00:05:11here add python 3.7 to path and on a
- 00:05:14windows computer path means
- 00:05:17uh you can run python in this case
- 00:05:19anywhere on your computer right you
- 00:05:21don't have to be in the python directory
- 00:05:24in order to run python if you don't
- 00:05:26check this box you have to be in the
- 00:05:28python directory in order to run python
- 00:05:30you can't just run it from anywhere so
- 00:05:32we want to be able to run it from
- 00:05:33anywhere right so go ahead and click
- 00:05:36that box make sure there's a little
- 00:05:37check box the check mark is is selected
- 00:05:40on there and it's so weird that by
- 00:05:42default this is not selected we
- 00:05:44absolutely want this checked
- 00:05:46and if you installed python some other
- 00:05:48time chances are you didn't click that
- 00:05:50little box because it's not the default
- 00:05:53you're not paying attention whoever
- 00:05:54reads the stuff on the installation
- 00:05:56screen you just click next right so
- 00:05:58there's a good chance that you don't
- 00:06:00have python installed on your path and
- 00:06:01we want to make sure that it is so go
- 00:06:03ahead and check that
- 00:06:04once you do that go ahead and click
- 00:06:06install now
- 00:06:07and i'm going to click no and cancel
- 00:06:09this thing because i've already
- 00:06:11installed python but you'll click ok and
- 00:06:14just wait for the installation process
- 00:06:16to complete it'll scroll through some
- 00:06:18stuff there'll be a little bar status
- 00:06:20bar and then that'll be that and you're
- 00:06:22good to go now in order to use python we
- 00:06:24don't have to launch python we don't
- 00:06:26have to start it we don't have to do
- 00:06:27anything at all we've added it to our
- 00:06:29path now we can just use it anytime we
- 00:06:31want anywhere we want always on our
- 00:06:34computer so very cool if you're on a mac
- 00:06:36i don't have instructions for you to
- 00:06:39install on a mac you can just go to
- 00:06:40youtube and type in python installation
- 00:06:43mac and you'll see a little two-minute
- 00:06:44video that'll show you if you don't
- 00:06:46already know yourself chances are you
- 00:06:48already do
- 00:06:49so we'll just leave it at that so that's
- 00:06:51all for this video in the next video
- 00:06:52we'll jump in and look at installing our
- 00:06:54text editor and our command prompt
- 00:06:56terminal
- 00:07:02so we've installed python now we want to
- 00:07:03install the text editor that we're going
- 00:07:06to use throughout this course and
- 00:07:09i am just using this it's the sublime
- 00:07:12text editor we'll take a look at it more
- 00:07:14detail in just a minute but it's sort of
- 00:07:16like the standard text editor most
- 00:07:18coders use it it's super easy to use it
- 00:07:20has all the bells and whistles that you
- 00:07:22would want and it's completely free now
- 00:07:24uh you can actually pay a little box
- 00:07:26will pop up every once in a while asking
- 00:07:28for a donation so if you want to throw
- 00:07:30those guys some money that's cool you
- 00:07:32absolutely don't have to you can use it
- 00:07:34forever for free just whenever that box
- 00:07:36pops up every few weeks or so just click
- 00:07:38no and that's that so to use this thing
- 00:07:41i'm just going to go to google and type
- 00:07:42in sublime text
- 00:07:44and the first thing that popped up is
- 00:07:46sublimetext.com
- 00:07:48or you could just go directly there and
- 00:07:50here's the website all you have to do is
- 00:07:51click download for windows right now
- 00:07:54it's sublime text
- 00:07:56three
- 00:07:57build 3207 if it's a different number by
- 00:08:00the time you watch this video no big
- 00:08:01deal whatsoever just download the latest
- 00:08:03version now i'm not going to install
- 00:08:04this because there's nothing to it uh
- 00:08:07just like in the last video with python
- 00:08:08the little thing will pop up down here
- 00:08:10you click it and just walk through the
- 00:08:13installation wizard i don't think
- 00:08:14there's anything you actually have to do
- 00:08:16i just
- 00:08:17click all the defaults ok and then
- 00:08:20install it and after you've installed it
- 00:08:22you can run it by going to your windows
- 00:08:24start menu and just typing in sublime
- 00:08:25and the little icon will pop up and you
- 00:08:27can click it so that's cool now the last
- 00:08:30thing we need and the only other thing
- 00:08:32we need is a terminal something to run
- 00:08:35commands on
- 00:08:36and
- 00:08:38it really doesn't matter what kind of
- 00:08:39terminal you use if you're on linux or
- 00:08:41mac they come with a terminal that's
- 00:08:42perfectly fine you can use that on a mac
- 00:08:44just go up the little search thing at
- 00:08:46the type top and type in terminal and
- 00:08:48it'll pop up
- 00:08:49you know if you're on windows there's a
- 00:08:52command prompt or a power shell you can
- 00:08:55probably use either of those i don't
- 00:08:57like using those i enjoy instead using
- 00:09:00the python or using the get bash
- 00:09:02terminal it allows us to do version
- 00:09:04control we're not going to do any
- 00:09:06version control in this course because
- 00:09:07there's not much to it
- 00:09:08code wise but if you've got a big
- 00:09:11project and you want to use git or
- 00:09:13github you need something that allows
- 00:09:15for that and this git bash terminal does
- 00:09:17so it's really nice and it's absolutely
- 00:09:19free so to use that i'm just going to go
- 00:09:21to google google and type in git bash
- 00:09:24that should be it and then here's the
- 00:09:26website get dash scm dot com forward
- 00:09:28slash downloads
- 00:09:30and so we can click
- 00:09:32download this and i will walk you
- 00:09:33through this one because it's a little
- 00:09:35weird so i'm just going to download this
- 00:09:36to
- 00:09:38the desktop
- 00:09:39and
- 00:09:40it takes a little bit longer for this to
- 00:09:41download it's only 44 megabytes
- 00:09:44so it should download immediately
- 00:09:45especially on a high speed internet like
- 00:09:47i have
- 00:09:48for some reason it takes a bit to
- 00:09:49download it always has
- 00:09:51something about this website or
- 00:09:52something it's just it's on a slow
- 00:09:54server or something and here we go it's
- 00:09:57downloaded so go ahead and click that
- 00:09:59and run it
- 00:10:03and let me pull this over
- 00:10:05and so okay it's just the license click
- 00:10:07next and you can leave all of these the
- 00:10:09defaults
- 00:10:11now here it's saying what text editor
- 00:10:13would you like to use now this is weird
- 00:10:15because we're not going to use this in a
- 00:10:17text editor you don't you use it it's a
- 00:10:19standalone thing right so
- 00:10:21we are going to use sublime text so i'll
- 00:10:23just leave this but you can just ignore
- 00:10:25this if you want
- 00:10:27just click next and here we want to use
- 00:10:30git and optional unix tools from the
- 00:10:32command prompt
- 00:10:34we're gonna like i said we're also we're
- 00:10:35gonna use the get batch terminal on its
- 00:10:37own so this again doesn't really matter
- 00:10:39but
- 00:10:40we'll leave this one clicked here at the
- 00:10:42bottom and click next
- 00:10:44open ssh this is for uh pushing stuff up
- 00:10:46to like github where you need an ssh key
- 00:10:49we're not going to use any of this
- 00:10:51doesn't matter we just click next same
- 00:10:52for this click next
- 00:10:54and this is a well this is a get thing
- 00:10:56we're not going to be using git really
- 00:10:58in this course
- 00:11:00so we can click next again
- 00:11:02same thing with this min tty thing click
- 00:11:04next
- 00:11:06basically we're just taking all of the
- 00:11:07defaults again click next
- 00:11:10and you get to this final thing and then
- 00:11:12go ahead and click install now i'm not
- 00:11:14going to do that because i've already
- 00:11:15installed it so i'll just click cancel
- 00:11:17but you can click install
- 00:11:19am i sure yes i am and it takes a couple
- 00:11:22of minutes to install it has a little
- 00:11:24you know
- 00:11:26status bar thing that'll scroll through
- 00:11:28and finish when it's finished and uh so
- 00:11:30yeah that's
- 00:11:31all there is to it so
- 00:11:33those are all the tools we need we don't
- 00:11:34have to download anything else in order
- 00:11:36to start using this so in the next video
- 00:11:38we'll jump right in and start writing
- 00:11:39some code and using the database
- 00:11:46all right so we've got all the tools we
- 00:11:48need all the things we have to download
- 00:11:50and install now we can just start using
- 00:11:52this database so
- 00:11:54most people don't realize that sql light
- 00:11:553 comes with python it's built in
- 00:11:58already so there's nothing to download
- 00:11:59there's nothing to install it's already
- 00:12:01there all you have to do is just sort of
- 00:12:04reference it in your code and start
- 00:12:06using it so
- 00:12:08in order to use sqlite3 all we have to
- 00:12:12do is import
- 00:12:13and then name it so it's sql ite 3 right
- 00:12:19so
- 00:12:20this is just a blank file that we've
- 00:12:22opened in sublime first thing we want to
- 00:12:24do is save this and we need to save this
- 00:12:26as a python file so i'm going to come up
- 00:12:28here to file
- 00:12:29and save as
- 00:12:31and
- 00:12:33go to our c directory here and we need
- 00:12:35to create a new directory in order to
- 00:12:37hold all the files that we're going to
- 00:12:38be creating throughout this course so
- 00:12:40i'm just going to right click and click
- 00:12:41new folder and let's just name this
- 00:12:44sqlite
- 00:12:46so then we can click on here
- 00:12:48and
- 00:12:49we can name this anything we want
- 00:12:51let's name it sqlite.pi
- 00:12:53well no
- 00:12:55we don't want to name it the same thing
- 00:12:56that we
- 00:12:58import here
- 00:13:00sqlite3 so let's just call this a
- 00:13:02database
- 00:13:04dot pi
- 00:13:06and you see as soon as we do that
- 00:13:08things sort of change color a little bit
- 00:13:11this becomes red that tells us that
- 00:13:14sublime now knows that this is a python
- 00:13:18program a python file that we've created
- 00:13:20right so that's cool so
- 00:13:22all right now
- 00:13:25we've imported sqlite3
- 00:13:27the next step is to create a connection
- 00:13:30and
- 00:13:30in order to do anything we need a
- 00:13:32connection to our database so we need a
- 00:13:36way for this file to sort of connect and
- 00:13:38do stuff so to do this really easy we
- 00:13:40just create a variable and you can call
- 00:13:41it anything you want but i'm just going
- 00:13:43to call this con short for connection
- 00:13:45and then we want to set this equal to
- 00:13:48sqlite3
- 00:13:51dot connect
- 00:13:53and then
- 00:13:54we want to pass in
- 00:13:56the name of the database that we want to
- 00:13:58create now
- 00:14:00this will connect to whatever we call
- 00:14:03the database and if it doesn't exist it
- 00:14:06will create it in this directory in this
- 00:14:09sqlite directory that we just we just
- 00:14:11created so we can call this anything we
- 00:14:13want let's just call it um
- 00:14:16customer.db we're going to pretend we
- 00:14:18have a company we have customers so
- 00:14:20we're going to create a database of
- 00:14:21those customers so
- 00:14:23that's
- 00:14:24pretty much all we need to do
- 00:14:26and
- 00:14:28one thing i will note
- 00:14:29sqlite3 allows you to create a database
- 00:14:31like we've just done or you could also
- 00:14:34just use a database in memory
- 00:14:37and
- 00:14:39it won't save it you can use it in your
- 00:14:42program but then as soon as the program
- 00:14:43ends the database disappears so you can
- 00:14:45do stuff to it but it's not permanent
- 00:14:48right so if you want to do that
- 00:14:50i'm just going to copy this
- 00:14:53and paste instead of naming the database
- 00:14:56oops go back
- 00:14:59instead of naming this database instead
- 00:15:02we just type in colon
- 00:15:04colon colon and then memory
- 00:15:08and then another colon
- 00:15:10right so this will create a connection
- 00:15:12to a database in memory now we're not
- 00:15:15going to do that in this course we want
- 00:15:16to actually save our database so we can
- 00:15:17use it later on if we want to but if
- 00:15:20you've just got some little thing you
- 00:15:21need to store some data do some stuff
- 00:15:23too and then delete it afterwards this
- 00:15:26memory thing will work so that's kind of
- 00:15:28cool i'll just comment that out
- 00:15:31okay so we've got our connection
- 00:15:33and if we save this i'm going to hit
- 00:15:35ctrl s on my keyboard
- 00:15:39now we can run this it won't actually do
- 00:15:41much well that's not actually true uh to
- 00:15:43run this head over to your windows start
- 00:15:45menu and type in git bash right just
- 00:15:48search for git bash and that will open
- 00:15:51up this
- 00:15:53and if we go pwd we could see we're in
- 00:15:55the c user's flat planet directory now
- 00:15:58my computer is called flat planet it's
- 00:16:00just the name of it i don't think the
- 00:16:01world is flat i just find it hilarious
- 00:16:03some people do so that's when i name my
- 00:16:05computer whatever your computer name is
- 00:16:07or whatever your username is you'll
- 00:16:08likely see that there so what we want to
- 00:16:10do first off is change directories we
- 00:16:13want to move into that directory we just
- 00:16:15created with our sublime text so to do
- 00:16:18that we use the cd command stands for
- 00:16:19change directory and then just type in c
- 00:16:23and then
- 00:16:25sqlite
- 00:16:26i think that's the name of the directory
- 00:16:28we just created with sublime if we type
- 00:16:30in ls we can see our database.pi file
- 00:16:34that we've just created and that is just
- 00:16:37this file right here right database.pi
- 00:16:40and there's not much going on in here
- 00:16:42just yet
- 00:16:43but we can still run this and to run it
- 00:16:46as long as we're in this directory we
- 00:16:48just type in python and then the name of
- 00:16:50the file which is database dot pi
- 00:16:53and if we do that it doesn't look like
- 00:16:55anything's happened but now if we type
- 00:16:57in ls
- 00:16:58we see this customer.db file it's been
- 00:17:01created and that is just this guy right
- 00:17:05here so
- 00:17:07the database wasn't wasn't there before
- 00:17:09but when we ran this connection it
- 00:17:11noticed that there wasn't a there wasn't
- 00:17:13a database called customarydb and we're
- 00:17:15trying to connect to customerdb so it
- 00:17:17just creates it for us so that's a real
- 00:17:19quick and easy way to create a database
- 00:17:21and that's all for this video in the
- 00:17:23next video we'll create a table and then
- 00:17:25we'll start adding data to that table
- 00:17:27and should be fun
- 00:17:33all right so we've created our database
- 00:17:34we have a connection now we want to
- 00:17:36build a table in order to you know put
- 00:17:39data in and if you're not really
- 00:17:41familiar with databases a database
- 00:17:44itself doesn't really do much it's the
- 00:17:46table inside that has all the stuff in
- 00:17:49it all the data goes into the table
- 00:17:51whenever you query a database you're
- 00:17:53querying a table whenever you're putting
- 00:17:55information into a database you're
- 00:17:56putting it into a table so it's the
- 00:17:59table that's the important thing of a
- 00:18:01database and think of a table as an
- 00:18:03excel spreadsheet it's just a big thing
- 00:18:06with rows and columns right that's
- 00:18:09pretty much it so if you can visualize a
- 00:18:11spreadsheet that's what a table is and
- 00:18:13so to create a table
- 00:18:16in uh sqlite 3 it's pretty simple first
- 00:18:19we need to create a cursor and
- 00:18:22a cursor
- 00:18:23is
- 00:18:25it's sort of like
- 00:18:26i don't know it's it's it's what tells
- 00:18:29the database what you want to do so
- 00:18:31whenever you do anything you're going to
- 00:18:32use your cursor to do it and you'll
- 00:18:34understand more exactly what that means
- 00:18:36as we start to use this it's really easy
- 00:18:38uh we just have to build it once and
- 00:18:40then we can use it so to to create a
- 00:18:42cursor
- 00:18:44we just create a variable and you can
- 00:18:45call it anything you want you could call
- 00:18:47it cursor if you wanted i'm just going
- 00:18:49to call it c because we're going to be
- 00:18:50typing it a lot so just c
- 00:18:52so c equals
- 00:18:55and we want to connect
- 00:18:58to our connection this con
- 00:19:01and then
- 00:19:02we want to create a cursor instance
- 00:19:04and you don't need to know what that
- 00:19:05means it's just
- 00:19:07it's the cursor right so we do that and
- 00:19:10let's make a comment and i'm going to go
- 00:19:12create
- 00:19:14a cursor
- 00:19:15right so now we can use this cursor to
- 00:19:18do all kinds of things and the first
- 00:19:21thing we want to do is create a table so
- 00:19:22let's go create a table
- 00:19:26now so to create a table it's really
- 00:19:28easy we use our cursor c
- 00:19:30and whenever we're we want to do things
- 00:19:33in the database we're almost always
- 00:19:35executing some command so we want to go
- 00:19:37c dot execute
- 00:19:40and then
- 00:19:41pass in whatever commands we want in
- 00:19:44this case we're going to create a table
- 00:19:46and there's a couple of different ways
- 00:19:48to execute commands using sql and the
- 00:19:50first one i'm going to show you is
- 00:19:52something called a
- 00:19:54a doc string i think this was yeah doc
- 00:19:56string and it's just
- 00:19:59six
- 00:20:00quotation marks
- 00:20:01right
- 00:20:02and inside of here
- 00:20:04you can do multiple things on multiple
- 00:20:07lines right that's why we want to use
- 00:20:09this docs string and this is what the
- 00:20:12python documentation recommends you do
- 00:20:14now a lot of times when we execute a
- 00:20:16command to our database we're just doing
- 00:20:18one thing right
- 00:20:20add
- 00:20:21bob to the database right so that's one
- 00:20:24line we won't use doc strings we'll just
- 00:20:26use regular quotation marks but in this
- 00:20:29instance when we're creating a whole
- 00:20:31table there's going to be a lot of data
- 00:20:32to push up we want to do it on multiple
- 00:20:34lines just so it's easier to read and so
- 00:20:37we'll use these docs strings so
- 00:20:39what we want to do is create
- 00:20:43a table
- 00:20:44and what do we want to call our table
- 00:20:46well our database is a customer database
- 00:20:48so let's create a table of customers
- 00:20:52plural so the customers table is in the
- 00:20:55customer
- 00:20:56singular database call it anything you
- 00:20:58want though it doesn't really matter and
- 00:21:00now
- 00:21:01all we have to do now
- 00:21:03is
- 00:21:05define
- 00:21:06the rows and columns that we want so
- 00:21:10this is going to be on several lines i'm
- 00:21:11just going to tab over here so what do
- 00:21:13we want well we want to record customers
- 00:21:17first names their last names and let's
- 00:21:19say email so
- 00:21:21we just sort of put all those things out
- 00:21:24we type in first
- 00:21:25and we want to call it first underscore
- 00:21:28name
- 00:21:28and then we have to decide the
- 00:21:32data type
- 00:21:33i'll just put data type for now we'll
- 00:21:35talk about data types in a minute so
- 00:21:37first name
- 00:21:39last underscore name
- 00:21:41data type
- 00:21:45and email
- 00:21:47or email address
- 00:21:49let's just do email and then data type
- 00:21:54okay
- 00:21:56now we can
- 00:22:00do this and let's
- 00:22:02tab these over again okay so
- 00:22:06i'll talk about data types in just a
- 00:22:07second but let's look at this here we're
- 00:22:10creating a table and one thing i should
- 00:22:12note sqlite3 is case sensitive so the
- 00:22:15things you type capitalized or lowercase
- 00:22:18they matter so this needs to be capital
- 00:22:20create table and then lowercase
- 00:22:23customers
- 00:22:24okay so let's look at this whole command
- 00:22:26if i copy this
- 00:22:28and print it again
- 00:22:30just to give you an example without the
- 00:22:32doc strings we could just use regular
- 00:22:34quotation marks
- 00:22:37but you see you got these big red angry
- 00:22:40error messages here in our sublime text
- 00:22:43that's because this all needs to go on
- 00:22:46one line
- 00:22:48if you use single quotation marks
- 00:22:53and let's see
- 00:22:57finally boom so okay
- 00:23:00this will work right
- 00:23:02this is equally valid this is valid and
- 00:23:05this is valid but just look at this
- 00:23:07right here like it's hard to read this
- 00:23:09right
- 00:23:10you know you gotta scroll
- 00:23:13you know it's all smooshed together it's
- 00:23:16just no good that's why i use these doc
- 00:23:18string
- 00:23:20triple quotation marks in this case so
- 00:23:22just a a quick little aside so all right
- 00:23:25we're almost there now we need to talk
- 00:23:27about data types and
- 00:23:29data types if you're familiar with
- 00:23:31programming a data type is a type of
- 00:23:34data so like think strings
- 00:23:36numbers integers
- 00:23:39booleans
- 00:23:40um python has a bunch of other data
- 00:23:42types like lists and
- 00:23:45dictionaries
- 00:23:47and things like that
- 00:23:49if you're familiar with
- 00:23:50data types it's the same thing here you
- 00:23:52have to define the type of data that's
- 00:23:55going to be in each of these fields so
- 00:23:57in our case first name
- 00:23:59last name and email those are all text
- 00:24:01type things so we need a take that we
- 00:24:03need to define a text data type now the
- 00:24:05thing about sql lite that's actually
- 00:24:07kind of cool i actually like is it only
- 00:24:09has five data types that you can choose
- 00:24:11from other databases mysql postgres any
- 00:24:14other database you can think of
- 00:24:16has
- 00:24:17dozens of data types and it can get
- 00:24:20confusing because you only really use a
- 00:24:21couple right
- 00:24:23if you think about it you're going to
- 00:24:24use text you're going to use numbers
- 00:24:26that's probably about it right so that's
- 00:24:28kind of cool that
- 00:24:30uh sqlite3 only has five and the five
- 00:24:33are null
- 00:24:37let's see
- 00:24:38and
- 00:24:39integer
- 00:24:40i'll talk about these in just a sec here
- 00:24:42uh real
- 00:24:45text
- 00:24:47and
- 00:24:49blob
- 00:24:51right so null just means
- 00:24:54does it exist or does it not exist if it
- 00:24:57doesn't exist it's null right if it does
- 00:25:00exist it's not null right an integer is
- 00:25:03a number a whole number 1 9 106 2
- 00:25:07million right as opposed to real which
- 00:25:11is a decimal
- 00:25:1210.5 right 1995. that's real right so if
- 00:25:16you're using something as a number just
- 00:25:19use integer if you have decimals use
- 00:25:22real text is just how it sounds text and
- 00:25:25finally blob
- 00:25:26is
- 00:25:28it's it's it's extort it's stored
- 00:25:30exactly as it is so think of like images
- 00:25:33an image might be a blob um an mp3 file
- 00:25:36a music file might be a blob right it's
- 00:25:39just a blob right so pretty simple
- 00:25:42in our case we're going to use
- 00:25:45text for all of these
- 00:25:47so we just type in text
- 00:25:52text and
- 00:25:55text
- 00:25:57and that's it
- 00:25:58right
- 00:25:59uh let's go
- 00:26:01data types
- 00:26:03right
- 00:26:04okay so we're almost there now
- 00:26:06there's one more step actually two more
- 00:26:08steps we need to do we've created this
- 00:26:11cursor
- 00:26:12right
- 00:26:13and now we've
- 00:26:15created a command for the cursor to do
- 00:26:18but our program hasn't actually executed
- 00:26:20that cursor command yet right in order
- 00:26:23to do that we need to commit this thing
- 00:26:25to the database and if you're familiar
- 00:26:27with other databases committing
- 00:26:29something to a database is a common
- 00:26:30theme here to do it we just commit our
- 00:26:33connection so con dot
- 00:26:36commit
- 00:26:38right and that's it this will now
- 00:26:40execute this guy right here push
- 00:26:43whatever we're trying to do into the
- 00:26:44database and do whatever we're trying to
- 00:26:47do
- 00:26:47finally the last thing we really need to
- 00:26:49do
- 00:26:50and let's go
- 00:26:51commit
- 00:26:52our
- 00:26:53command
- 00:26:55the last thing we need to do is
- 00:26:57close our
- 00:26:59connection
- 00:27:01so anytime
- 00:27:03you create a connection like we did up
- 00:27:04here right you want to close that
- 00:27:07connection you don't have to when your
- 00:27:09file stops running it will by default
- 00:27:12close itself but it's just best practice
- 00:27:14to do it explicitly and to do that we
- 00:27:17just type in con.close
- 00:27:20right
- 00:27:21okay so if we save this
- 00:27:24and now run it
- 00:27:25pull up our terminal
- 00:27:27clear the screen and i'm just going to
- 00:27:29push up on my keyboard and that will
- 00:27:32scroll to previous commands and we just
- 00:27:34type in python database dot pi which is
- 00:27:36the name of our file and we hit this
- 00:27:38boom nothing has happened on the screen
- 00:27:41but
- 00:27:43uh
- 00:27:44we didn't get any error messages so that
- 00:27:46means that this thing executed correctly
- 00:27:48we've now created a table called
- 00:27:50customers inside of that customers table
- 00:27:53there are three columns first name last
- 00:27:55name and email and now we can start
- 00:27:57sending data into the database and we'll
- 00:28:00look at how to do that in the next video
- 00:28:06okay so we've got our table created now
- 00:28:08we want to put data into the table so
- 00:28:11how do we do that well
- 00:28:13pretty simple we just create another
- 00:28:16cursor
- 00:28:17command right so c dot execute
- 00:28:20anytime we do anything we want to
- 00:28:23execute
- 00:28:24our c our cursor right and so now
- 00:28:28we want to insert data into our table so
- 00:28:32we use the insert into sql command and
- 00:28:36this is all just sql if you're familiar
- 00:28:38with sql sql these are
- 00:28:41just sql commands right very basic so
- 00:28:44they're always capitalized like that so
- 00:28:46we want to insert into
- 00:28:48customers the name of our table
- 00:28:50and then values
- 00:28:53and then
- 00:28:54just
- 00:28:57type in whatever values we want so
- 00:28:59remember we have first name last name
- 00:29:01and email so we need three things right
- 00:29:04so
- 00:29:06we want let's go
- 00:29:09john
- 00:29:11and we separate each of these by a comma
- 00:29:13elder
- 00:29:15comma
- 00:29:17and then john at codamay.com
- 00:29:20right so
- 00:29:21you notice i'm using single quotes
- 00:29:23that's because we started this string
- 00:29:25off with a double quote so if we used if
- 00:29:28we tried to use a
- 00:29:29double quote here the whole thing would
- 00:29:31be wonky because it would think that
- 00:29:34we're trying to close this double quote
- 00:29:36instead we just use single quotes and
- 00:29:38everything works fine so let's see that
- 00:29:40worked
- 00:29:42that looks good
- 00:29:43now again we need to commit
- 00:29:47this command so we'll just leave that
- 00:29:48there and we need to close our
- 00:29:50connection so let's go ahead and save
- 00:29:51this
- 00:29:53come back here and
- 00:29:55run this again and again we get no
- 00:29:57message now we could create a little
- 00:29:59message if we wanted to
- 00:30:01you know we could just print to the
- 00:30:02screen
- 00:30:05command executed
- 00:30:09successfully
- 00:30:12i don't know you know if we wanted to do
- 00:30:13that we could we definitely don't have
- 00:30:15to do that so we've now inserted one
- 00:30:18record one
- 00:30:20row into our database we could do more
- 00:30:23than that we could go tim
- 00:30:25tim
- 00:30:27smith
- 00:30:29let's go tim at codamy.com so if we save
- 00:30:33this come back here run it again
- 00:30:36command execute it successfully that
- 00:30:38makes you feel a little better that
- 00:30:40something is on the screen right
- 00:30:42whatever uh let's do one more let's go
- 00:30:45uh mary
- 00:30:48that's a good last name
- 00:30:50mary
- 00:30:52brown
- 00:30:53i don't know
- 00:30:54and she's at mary kotami.com
- 00:30:58save this
- 00:31:00run it again
- 00:31:03okay so now we've got three records in
- 00:31:05our database
- 00:31:07pretty simple
- 00:31:09to sort of add one thing at a time to
- 00:31:11our database
- 00:31:12and uh yeah that's all for this video
- 00:31:20okay in the last video we inserted one
- 00:31:22value at a time and sometimes you want
- 00:31:24to do that but sometimes you want to
- 00:31:27sort of insert lots of things all at
- 00:31:29once how do you do that well it's a
- 00:31:31little bit more complicated but not too
- 00:31:32much more complicated so let's create
- 00:31:36um i don't know many
- 00:31:40customers
- 00:31:41a variable and we want this to be a
- 00:31:43python list right so inside of here
- 00:31:48each
- 00:31:49item
- 00:31:51as you would with any python item
- 00:31:54you can
- 00:31:55go like this
- 00:31:57right so each of these things become an
- 00:32:00item
- 00:32:01right
- 00:32:02so it's like a tuple inside of a list
- 00:32:04right and here you would just go
- 00:32:08let's go wes
- 00:32:10at
- 00:32:12brown
- 00:32:15and let's go wes at
- 00:32:19brown.com right
- 00:32:23and we can put these on multiple lines
- 00:32:25if we want
- 00:32:33to sort of just make it easier to read
- 00:32:35we can go
- 00:32:36um
- 00:32:38i don't know
- 00:32:40staff
- 00:32:43uh
- 00:32:45[Music]
- 00:32:47steph at
- 00:32:53and finally we can go
- 00:32:56dan
- 00:32:59pass
- 00:33:02and dan's email address is dan at
- 00:33:05pass.com
- 00:33:07all right
- 00:33:09i'm gonna go ahead and put a
- 00:33:12comma there at the end of that okay so
- 00:33:14now
- 00:33:15we need to do a little bit
- 00:33:18different down here in our execute
- 00:33:19instead of executing we're going to
- 00:33:22execute many
- 00:33:23right
- 00:33:24slightly different command there
- 00:33:26and here instead of
- 00:33:28typing in
- 00:33:30all the things we want to
- 00:33:33create a tuple and we want to put
- 00:33:35placeholders so the placeholder in sql
- 00:33:37i3 is the
- 00:33:38question mark so we've got a question
- 00:33:40mark question mark question mark so this
- 00:33:43is sort of first name last name email
- 00:33:45right
- 00:33:47and then
- 00:33:48we put a comma and we just pass in the
- 00:33:51name of our list here right many
- 00:33:53customer
- 00:33:55actually this changes many customers
- 00:33:59makes more sense right
- 00:34:00okay so
- 00:34:03what's going to do here is it's going to
- 00:34:04execute many
- 00:34:06each of these is one two three
- 00:34:09and
- 00:34:10here's the placeholder for each one
- 00:34:13pretty simple if we save this
- 00:34:16and run it again
- 00:34:19command executed successfully so
- 00:34:21hopefully that has worked we don't know
- 00:34:23yet if any of these have worked because
- 00:34:25we haven't actually looked inside of our
- 00:34:27database to see what's in there and
- 00:34:29we'll do that in the next video
- 00:34:35okay so we've put a bunch of stuff into
- 00:34:37the database now how do we get anything
- 00:34:38out and sort of display it see what's in
- 00:34:40there well to do that we need to query
- 00:34:42the database
- 00:34:44and to do that we use our cursor
- 00:34:45c.execute
- 00:34:47there we go
- 00:34:48and then inside of here we want to
- 00:34:51select
- 00:34:54and let's just pull out everything at
- 00:34:56this point so star stands for everything
- 00:34:58from
- 00:34:59and we want to pull it out from the
- 00:35:01customers
- 00:35:05table
- 00:35:06all right so
- 00:35:08that
- 00:35:09sort of creates the query
- 00:35:11and
- 00:35:12to actually
- 00:35:14get the thing we need to then go c
- 00:35:18dot
- 00:35:19fetch
- 00:35:20and
- 00:35:21you can do three different things i'll
- 00:35:23just type them out we can fetch one
- 00:35:26we can c dot fetch
- 00:35:29many and then pass in how many however
- 00:35:32many we want or we can c dot
- 00:35:35fetch
- 00:35:36all
- 00:35:37and this will return as you would expect
- 00:35:40everything this will just fetch the last
- 00:35:42the last item in the table
- 00:35:45yeah uh so what we want to do is fetch
- 00:35:48all so if we run this nothing actually
- 00:35:51happens we have to actually print to the
- 00:35:54screen explicitly so we would wrap this
- 00:35:56whole thing in a print function
- 00:35:59and then we can go ahead and
- 00:36:03comment out that so let's save this and
- 00:36:05run it and see what we got here
- 00:36:09and boom we get all of this stuff and
- 00:36:11this is returned as
- 00:36:14a python list right so here we have john
- 00:36:17elder that was the first one tim smith
- 00:36:20mary brown
- 00:36:22and then
- 00:36:24that time in the last video when we did
- 00:36:26them all at once we had wes steph and
- 00:36:29dan
- 00:36:30and you can see all of them
- 00:36:33are uh sort of
- 00:36:36just sort of spewing up on the screen
- 00:36:37right so this is just all the data
- 00:36:40given at once
- 00:36:41obviously you can do all kinds of stuff
- 00:36:43to pull out specific stuff to make this
- 00:36:46look more pre readable pretty you know
- 00:36:49useful and i think we'll look at that in
- 00:36:51the next video
- 00:36:57okay so we fetched all right right off
- 00:36:59the bat
- 00:37:01it spewed back all this stuff we could
- 00:37:03also
- 00:37:05let's go ahead and comment this out
- 00:37:08and we could just fetch one and if we
- 00:37:10want to print this to the screen we wrap
- 00:37:12it all on print function we save this
- 00:37:14and run it just real quick just to show
- 00:37:16you what this other stuff does
- 00:37:18boom john elder john codamy.com
- 00:37:21you can see it looks like a tuple tuple
- 00:37:23however you want to say that and you can
- 00:37:25access it just as a tuple so you notice
- 00:37:28this is the very first thing so it's not
- 00:37:30the last thing that fetch one brings you
- 00:37:32it's the first thing right
- 00:37:34likewise you could do
- 00:37:38let's see fetch many
- 00:37:41let's comment this out and we want to
- 00:37:42wrap this
- 00:37:46save this and run it
- 00:37:49we get three
- 00:37:51one two and three it's just the first
- 00:37:53second and third one right so kind of
- 00:37:56interesting
- 00:37:58but
- 00:37:58we don't wanna do that we wanna work
- 00:38:00with fetch all
- 00:38:02and now we to do stuff
- 00:38:04right
- 00:38:05so
- 00:38:06well actually first before we do that
- 00:38:09let's go back to the fetch one
- 00:38:12now this is
- 00:38:15a tuple
- 00:38:17so we can access the stuff inside of it
- 00:38:19by referencing
- 00:38:21the the item number so first off before
- 00:38:23we do that let's just save this and run
- 00:38:24it again just to show you
- 00:38:26real quick i'm gonna clear the screen
- 00:38:30okay so fetch one brings
- 00:38:33one record john elder and john
- 00:38:34codomy.com so this is the zero with item
- 00:38:37in the tuple this is the first item
- 00:38:40and this is the second item this is
- 00:38:42python tuple stuff you're familiar with
- 00:38:43tuples or lists when you number items in
- 00:38:46a list or a tuple the first one is zero
- 00:38:49the second one is one and the third one
- 00:38:52is two right so we can access specific
- 00:38:54ones of those by referencing those
- 00:38:56numbers and to do that
- 00:38:58we just like i said slap on the
- 00:39:01brackets here and let's just call zero
- 00:39:03the zero with item that should be john
- 00:39:05so if we save this and run it
- 00:39:07boom we get john so that is one way to
- 00:39:09do it right and you can do the same
- 00:39:11thing with fetch
- 00:39:13fetch all
- 00:39:14and fetch many for that matter but
- 00:39:17that's not really what we want to look
- 00:39:19at right now
- 00:39:20what we want to look at is fetch all
- 00:39:23and
- 00:39:26this is
- 00:39:27a list right so we could
- 00:39:32set a new variable we could create a
- 00:39:34variable called um
- 00:39:36items
- 00:39:37and set it equal to this
- 00:39:40then we could either just print
- 00:39:42items
- 00:39:44in fact if we do this
- 00:39:47it'll look the same
- 00:39:48save this and run it as it did earlier
- 00:39:51just spews out all of it right
- 00:39:54or you could
- 00:39:57create a loop of some sort to loop
- 00:39:59through each thing
- 00:40:01let's go ahead and give that a try
- 00:40:04so let's go
- 00:40:07for item in items
- 00:40:11print
- 00:40:13item
- 00:40:15okay so if we save this
- 00:40:18and run it just a basic for loop boom we
- 00:40:22have each of these printed out
- 00:40:25one line at a time now we're starting to
- 00:40:27get somewhere it's starting to be a
- 00:40:29little bit more readable right so each
- 00:40:31of these are now a tuple and we can do
- 00:40:34tupley things to them so instead of
- 00:40:38printing out the whole item
- 00:40:40we could print out
- 00:40:42use our brackets and say print out the
- 00:40:44zeroth item inside each tuple all right
- 00:40:47so if we save this
- 00:40:49and run it
- 00:40:52we get john 10 mary west steph and dan
- 00:40:55very cool
- 00:40:57there are three
- 00:41:00columns so we could if we really wanted
- 00:41:03to play around with this we could go
- 00:41:05print
- 00:41:06this
- 00:41:07plus we can then concatenate
- 00:41:10and let's make a space and concatenate
- 00:41:12again and let's go item
- 00:41:16one
- 00:41:17concatenate
- 00:41:18make another space
- 00:41:20and then concatenate again and go item
- 00:41:242
- 00:41:25right so if we save this and run it
- 00:41:29and let's clear the screen
- 00:41:32we get john elder john codyme.com tim
- 00:41:35smith right so it's starting to format
- 00:41:37up a little bit
- 00:41:39and we could play around with this you
- 00:41:40know we could uh do a little formatting
- 00:41:45like that
- 00:41:47if we save this and run it
- 00:41:51and that's looking a little bit better
- 00:41:53we could um oh i don't know we could use
- 00:41:56tabs instead
- 00:41:59that
- 00:42:00is backslash t
- 00:42:04is that right there's a forward slash
- 00:42:07backslash so this will tab everything
- 00:42:09over
- 00:42:11so if we
- 00:42:12run this
- 00:42:14and now things are starting to look a
- 00:42:16little bit better this one didn't quite
- 00:42:17make it over because this
- 00:42:20it did tab but it
- 00:42:22this name is so short the tab didn't go
- 00:42:25as far
- 00:42:26that's kind of funny
- 00:42:30we could do
- 00:42:32two tabs i suppose
- 00:42:35right
- 00:42:36and here we could go
- 00:42:39outside of the loop
- 00:42:41we could go
- 00:42:42name
- 00:42:45well we need quotation marks name
- 00:42:50yeah
- 00:42:52plus
- 00:42:54two tabs
- 00:42:57and email
- 00:43:00so i'll look right
- 00:43:02and here we could do print and
- 00:43:05one two three four
- 00:43:08five
- 00:43:10plus
- 00:43:13a couple of tabs
- 00:43:15and then what one two three four five
- 00:43:17six
- 00:43:20seven
- 00:43:21eight
- 00:43:22i don't know
- 00:43:23a couple more boom
- 00:43:25i don't know save this i'm just playing
- 00:43:26around at this point because it's kind
- 00:43:28of fun
- 00:43:30all right formatting has gone a little
- 00:43:31bit wonky here this is too short
- 00:43:35but you get the idea right you can sort
- 00:43:37of
- 00:43:39play around with all this stuff
- 00:43:41let's get rid of
- 00:43:45one of these tabs see how that looks
- 00:43:50clear the screen real quick
- 00:43:53all right so looking a little better
- 00:43:55the point is you can use all the sort of
- 00:43:57pythonic things you want loops
- 00:44:00things like that to format the
- 00:44:02stuff that you fetched all
- 00:44:04in any way you want and you use it just
- 00:44:06by calling any list item in the returned
- 00:44:10data so
- 00:44:12a little bit
- 00:44:14much to to
- 00:44:16swallow in this one but uh really pretty
- 00:44:18simple when you get right down to it all
- 00:44:20the stuff at the end which is playing
- 00:44:21around here so
- 00:44:22that's how you
- 00:44:24sort of format results
- 00:44:26using python and sql i3
- 00:44:32okay in this video i want to talk about
- 00:44:34primary keys and a primary key is a
- 00:44:37database thing if you're familiar with
- 00:44:39other databases you're probably already
- 00:44:40aware of what a primary key is and
- 00:44:43basically it's a unique id number that
- 00:44:45each record in your database gets so
- 00:44:49for instance we've got
- 00:44:52we've select everything from our data
- 00:44:54database table customers and we've put
- 00:44:57it into this items variable now we could
- 00:45:00print out items right if we save this
- 00:45:04and run it
- 00:45:07oops
- 00:45:08there we go we see each of these items
- 00:45:11john elder
- 00:45:12tim blah blah blah we can like we did in
- 00:45:15the last video a couple of videos
- 00:45:17instead we can go
- 00:45:19um
- 00:45:20for item in
- 00:45:22items
- 00:45:25print
- 00:45:26item
- 00:45:27if we save this it looks a little bit
- 00:45:29nicer
- 00:45:31right so each of these is a record this
- 00:45:34is a record this is a record this is a
- 00:45:36record right and you know each record
- 00:45:39has a column first name a column last
- 00:45:41name and a column email well
- 00:45:43actually sqlite 3 creates another column
- 00:45:47sort of in the background with a
- 00:45:49specific primary key a unique id that
- 00:45:52is unique for each of these records and
- 00:45:55to see what that is it's called a row id
- 00:45:58and we can actually select
- 00:46:01right up here in our select statement we
- 00:46:03can just go row id
- 00:46:05and everything from customers now if we
- 00:46:08save this
- 00:46:09and run it
- 00:46:11it's going to print out that id as well
- 00:46:13so you can see one two three four five
- 00:46:15six
- 00:46:17john
- 00:46:18two tim three mary so you know
- 00:46:21we could look up mary brown or we could
- 00:46:24look up
- 00:46:25number three right and we'll look at how
- 00:46:28to
- 00:46:28call specific
- 00:46:30things from our database in the next
- 00:46:32video but in this video i just wanted to
- 00:46:34really quickly touch base on that
- 00:46:35primary key because it's an important
- 00:46:36database concept and in most databases
- 00:46:39you have to create the primary key
- 00:46:41yourself and then create some sort of
- 00:46:43auto incrementing mechanism so that
- 00:46:46anytime you add a new record to the
- 00:46:47database it auto increments a new id so
- 00:46:51for instance we have six right six is
- 00:46:53our last id if we added a new person in
- 00:46:56we would need to create something that
- 00:46:57said okay
- 00:46:58the last one was six so make the next
- 00:47:00one seven right we don't have to do any
- 00:47:02of that with sql light it does it for us
- 00:47:04automatically unless you specifically
- 00:47:06tell it not to and i'm not even going to
- 00:47:09get into showing you how to do that
- 00:47:10because
- 00:47:11you're never not going to want to do
- 00:47:12that you're always going to want this id
- 00:47:14it it's a good idea to have a unique
- 00:47:18number for each record that you can
- 00:47:19reference and we'll see why a little bit
- 00:47:21later on in the course so i just really
- 00:47:23quickly wanted to show you that row id
- 00:47:26uh it's just sort of a unique sql light
- 00:47:283 3 thing it does it for you it's very
- 00:47:31very cool
- 00:47:32and uh yeah so that's all for this video
- 00:47:33in the next video we'll look at pulling
- 00:47:35out specific things from our database
- 00:47:38like searching for specific things and
- 00:47:40that'll be in the next video
- 00:47:46okay up until now we've just been
- 00:47:47pulling everything from the database
- 00:47:50from the table now we want to look at
- 00:47:52how to pull out specific things how to
- 00:47:54search for things right so you maybe
- 00:47:56don't want everybody you might want
- 00:47:58uh just everybody with the name last
- 00:48:01name elder how would you do that well
- 00:48:03come up to our query here and so we want
- 00:48:06to let's get rid of the row id we don't
- 00:48:08really need that we want to select
- 00:48:09everything from customers and now we
- 00:48:12just use the where clause where this is
- 00:48:14just basic sql where
- 00:48:17we could go last underscore name
- 00:48:20equals
- 00:48:22and use single quotation marks
- 00:48:25uh elder right
- 00:48:27so if we save this and run it
- 00:48:32let's clear the screen here
- 00:48:35we get boom john elder
- 00:48:38that's cool
- 00:48:39now
- 00:48:40we can also use all of our other sort of
- 00:48:43comparison operators right if we had
- 00:48:45numbers we could use uh less than equal
- 00:48:49to
- 00:48:51greater than greater than or equal to
- 00:48:54you know where let's say if we had an
- 00:48:56age column we don't but if we had an age
- 00:48:58column where age is greater than let's
- 00:49:00say we wanted everybody over the age of
- 00:49:0221 right
- 00:49:03or equal to 21. we would do that right
- 00:49:06so that's kind of cool one you're going
- 00:49:08to use a lot is like so we could go
- 00:49:12where
- 00:49:13last underscore name is like
- 00:49:16and then use quotation marks to sort of
- 00:49:19search and we've got a bunch of browns
- 00:49:21so we could go where the name is sort of
- 00:49:22like br and then you put this uh
- 00:49:26what is that the percentage sign
- 00:49:29yeah to it's sort of like a wild card
- 00:49:31where it starts with br and ends with
- 00:49:34whatever right so if we save this and
- 00:49:37run it
- 00:49:40we get mary brown and wes brown right so
- 00:49:44uh very very cool
- 00:49:46uh we've got a bunch of kodamies right
- 00:49:49we could play around with that where we
- 00:49:52can go where email
- 00:49:54is like and we can start out with this
- 00:49:57percentage sign and then we can go
- 00:49:59kodamy.com
- 00:50:02save this
- 00:50:04i'm not sure why that turned purple like
- 00:50:07that
- 00:50:08but we get
- 00:50:10three returned results
- 00:50:12uh let's look at this more let's go
- 00:50:14let's get rid of this yeah it's just
- 00:50:16gonna do that it looks like so okay so
- 00:50:19there were three now we can test this
- 00:50:21just to make sure
- 00:50:22that was correct right
- 00:50:25and let's save this and just run it
- 00:50:26again
- 00:50:29so we have one two three
- 00:50:32and that's all
- 00:50:34so that was correct so a pretty simple
- 00:50:36way
- 00:50:38to
- 00:50:39search for different things using your
- 00:50:41query using the where clause and like i
- 00:50:44said you're going to use your logical
- 00:50:45operators
- 00:50:46those are basically
- 00:50:49you know
- 00:50:51like i said equal to
- 00:50:53less than greater than less than or
- 00:50:55equal to greater than or equal to
- 00:50:58uh like
- 00:50:59and that's pretty cool so in the next
- 00:51:01video we'll look at updating records
- 00:51:03making changes once we've already put
- 00:51:05something in the database and that'll be
- 00:51:06in the next video
- 00:51:12okay in this video we want to update
- 00:51:14records so
- 00:51:16let's head over here let's just put this
- 00:51:18stuff down and let's go update records
- 00:51:24and it's pretty simple pretty
- 00:51:25straightforward
- 00:51:26and we just go see
- 00:51:29there we go c dot execute
- 00:51:33right now this is going to actually be
- 00:51:35kind of long so we're going to use those
- 00:51:36doc strings again
- 00:51:38one two three four five six
- 00:51:40right
- 00:51:42and what we want to do is update
- 00:51:46and we want to update our customers
- 00:51:48table right now we want to set what do
- 00:51:52we want to set well let's say we want to
- 00:51:53change the first name of john elder to
- 00:51:57bob elder right so we want to set
- 00:52:00first name
- 00:52:03equal to
- 00:52:05bob
- 00:52:07and then
- 00:52:09where
- 00:52:12and let's go last name
- 00:52:15equals
- 00:52:16elder right
- 00:52:23and
- 00:52:25actually we want to wrap these in
- 00:52:28single quotes
- 00:52:31there we go
- 00:52:34boom
- 00:52:37ah come back
- 00:52:39i cannot type there we go all right so
- 00:52:41we could put this all in one line but
- 00:52:43easier to break it apart like that so
- 00:52:45this will work but it's probably not the
- 00:52:47way you want to do it we'll talk about
- 00:52:49that in just a second so let's go ahead
- 00:52:51and
- 00:52:52we want to
- 00:52:56commit this
- 00:52:58so we'll go see commit
- 00:53:00or con.commit right and then we want to
- 00:53:04select everything from our database and
- 00:53:06print it all onto the screen just to
- 00:53:07make sure that the change got made right
- 00:53:09so let's save this
- 00:53:11head back over here and run it again and
- 00:53:14boom we get bob elder
- 00:53:16tim smith everything else is the same
- 00:53:19and it looks like it's worked
- 00:53:20now
- 00:53:23this isn't a good way to do it
- 00:53:25i mean it works and this is the correct
- 00:53:28structure and everything but you're not
- 00:53:30going to want to make a change to a
- 00:53:31record
- 00:53:32based on a last name because we could
- 00:53:34have a bunch of elders in there and if
- 00:53:36we did that this would change them all
- 00:53:39and we don't want to necessarily do that
- 00:53:40right so instead
- 00:53:45we want to come down here
- 00:53:47and let's call our row id
- 00:53:50again
- 00:53:51let's save this and run it
- 00:53:56and now we see
- 00:53:58our row ids what you really want to do
- 00:54:00is use those row ids for this exact
- 00:54:02thing right we want we don't want to
- 00:54:04update all the records that have a last
- 00:54:07name elder we just want to update record
- 00:54:10number one so instead we want to do it
- 00:54:13that way so let's let's go ahead and try
- 00:54:14that again and up here we want to set
- 00:54:17first name equal let's change it back to
- 00:54:19john
- 00:54:20where
- 00:54:21and then we go
- 00:54:23row id
- 00:54:25equals
- 00:54:26one all right so if we save this
- 00:54:29and run this again
- 00:54:32boom
- 00:54:33now
- 00:54:35it's john elder
- 00:54:36right
- 00:54:37so like down here brown
- 00:54:40we want to change mary brown to marty
- 00:54:42brown right so she's number three if
- 00:54:45instead we did brown that would throw
- 00:54:48off some problems right so let's try
- 00:54:50that and just see what happens so
- 00:54:52we want to go marty
- 00:54:54where
- 00:54:59last underscore name
- 00:55:01equals brown
- 00:55:03right
- 00:55:04so if we save this and run it
- 00:55:10boom this one is marty brown and this
- 00:55:12one is marty brown see
- 00:55:15that's no good we can't have that right
- 00:55:18so
- 00:55:18instead
- 00:55:20let's change it back to mary
- 00:55:23and we can do that like this
- 00:55:27save this and run it
- 00:55:31that gets us part of the way now they're
- 00:55:32both mary brown but we want number four
- 00:55:35to beat wes brown
- 00:55:36right instead of mary so
- 00:55:39we would come up here
- 00:55:41where
- 00:55:43row i d
- 00:55:45equals four
- 00:55:48is that right
- 00:55:52which was wes west was plus yep four
- 00:55:55all right so then we want to change it
- 00:55:57to
- 00:55:58wes
- 00:55:58so if we save this
- 00:56:01let's clear the screen
- 00:56:03and run this one more time
- 00:56:06we get number three mary brown number
- 00:56:08four wes brown now everything is right
- 00:56:12in the world so important that you use
- 00:56:14id the row id the primary key in order
- 00:56:17to
- 00:56:18update things also to delete things
- 00:56:20we'll look at that next
- 00:56:22uh you know just very important
- 00:56:24otherwise you can get in all kinds of
- 00:56:25problems like we just saw
- 00:56:27and that's no good so that's all for
- 00:56:28this video in the next video we'll look
- 00:56:30at how to delete a record
- 00:56:36okay in the last video we looked at
- 00:56:37updating records in this video we want
- 00:56:39to look at deleting and deleting is very
- 00:56:41similar to updating it's pretty simple
- 00:56:43we just
- 00:56:45come over here and go c dot execute
- 00:56:49and then inside of here we want to
- 00:56:51delete
- 00:56:53from
- 00:56:54our customers
- 00:56:56table
- 00:56:57and just use our where clause where
- 00:57:00and then what
- 00:57:01well
- 00:57:05our data is gone
- 00:57:09but
- 00:57:10the last row
- 00:57:12was that dan
- 00:57:15guy he was number six right and uh let's
- 00:57:18get rid of him we had trouble with the
- 00:57:20formatting because his name was too
- 00:57:22small way back at the beginning of the
- 00:57:23course so let's just get rid of him so
- 00:57:25here we go row
- 00:57:26id equals 6. now you'll notice
- 00:57:29i'm not wrapping this in quotation marks
- 00:57:32because that would make this a string
- 00:57:35and the row id is a number it's an
- 00:57:36integer so you can't use a string when
- 00:57:38it's a number so just sort of keep that
- 00:57:41in mind and i think that's pretty much
- 00:57:43it so if we save this
- 00:57:46head over here and run it again
- 00:57:48boom number six dan pass is gone
- 00:57:52so that's delete
- 00:57:54once you delete a thing it's gone uh dan
- 00:57:57has been dropped from the table and when
- 00:57:58you delete things we call it dropping
- 00:58:00from a table so he's been dropped he's
- 00:58:02deleted he's gone
- 00:58:04and uh that's that he can't come back so
- 00:58:06keep that in mind when you delete
- 00:58:07something it's sort of permanent so uh
- 00:58:10yeah just keep that in mind oh so that's
- 00:58:12all there is to delete pretty simple and
- 00:58:14in the next video we'll look at ordering
- 00:58:20okay in this video i want to look at
- 00:58:22ordering and ordering allows us to
- 00:58:24return the results however we want right
- 00:58:27now we're just returning them by default
- 00:58:29which is just in order one two three
- 00:58:32four five right so we
- 00:58:34could order by just about anything
- 00:58:38and um
- 00:58:40well we'll just go through and do some
- 00:58:42and you'll see so let's see let's change
- 00:58:45this to
- 00:58:47order by
- 00:58:49in fact we can get rid of all this
- 00:58:53instead of query
- 00:58:56the database let's go
- 00:58:58order by
- 00:59:00all right so same sort of thing right we
- 00:59:03want to select row id and everything
- 00:59:06from customers
- 00:59:07and we just go order
- 00:59:11by
- 00:59:12and then let's go
- 00:59:15row id
- 00:59:17right so if we save this and run it
- 00:59:18we're not really going to see anything
- 00:59:20different here
- 00:59:22right one two three four five
- 00:59:24because default that's how things go
- 00:59:27ascending from small to high
- 00:59:30that's ascending
- 00:59:31or
- 00:59:32uh a
- 00:59:33asc ascending short for a sen
- 00:59:37ding right you can also do descending d
- 00:59:40e s c
- 00:59:42short for descending right
- 00:59:44and descending is high to low so we can
- 00:59:47do that
- 00:59:48just by changing this
- 00:59:50to desc
- 00:59:52all right so if we save this and run it
- 00:59:56boom now
- 00:59:58see the first one listed is five four
- 01:00:00three two one right
- 01:00:02pretty cool
- 01:00:04you know you could
- 01:00:06type in asc here but the default is
- 01:00:09ascending so you don't have to be
- 01:00:11explicit right
- 01:00:13we could order by last name
- 01:00:18now that's going to be alphabetical from
- 01:00:21low to high or from a to z right so if
- 01:00:25we save this and run it let's clear the
- 01:00:28screen
- 01:00:30we get three four one five two but
- 01:00:32you'll notice brown b
- 01:00:35so a b
- 01:00:36c d
- 01:00:37e
- 01:00:39f
- 01:00:39g h i j k l m n l p q r s
- 01:00:45and it's like that so
- 01:00:46uh let's see
- 01:00:48that was ascending we could do the
- 01:00:50opposite descending from z down to a
- 01:00:53right
- 01:00:54so we save this and run it
- 01:00:58we now have the s k e
- 01:01:01b
- 01:01:02and b and
- 01:01:04let's see
- 01:01:06i don't know how they determine which of
- 01:01:08the browns go first since they're both
- 01:01:10the same
- 01:01:11i would guess it's uh
- 01:01:13numerically but we're descending so i
- 01:01:15don't know
- 01:01:18interesting
- 01:01:19so that's order by relatively simple
- 01:01:22and
- 01:01:23yeah not much to it in the next video
- 01:01:26we'll look at and and or
- 01:01:32okay in this video i want to look at and
- 01:01:34and or and and and or allow us to sort
- 01:01:36of extend the functionality of your
- 01:01:38where clause so
- 01:01:41you know
- 01:01:42let's just come right in here and let's
- 01:01:44go where
- 01:01:46so
- 01:01:47and and or allow you to add more
- 01:01:49conditions to your where clause
- 01:01:52basically to search for more than one
- 01:01:53thing so let's look at our
- 01:01:57data real quick so we've got two of
- 01:01:59these browns right so
- 01:02:01let's search for
- 01:02:05where
- 01:02:06last underscore name
- 01:02:08is like
- 01:02:11and then
- 01:02:12let's go br and then that remember that
- 01:02:15percentage sign
- 01:02:17and
- 01:02:18right and where row id
- 01:02:23well actually first let's just run this
- 01:02:25by itself just to make sure this works
- 01:02:26right so here
- 01:02:29boom we get mary and wes brown now let's
- 01:02:32say we only want mary
- 01:02:35but we're not sure so we have to search
- 01:02:37also so we could go
- 01:02:40where
- 01:02:42last name is like bruh
- 01:02:44and
- 01:02:47row id
- 01:02:49equals three right
- 01:02:52so notice we have two
- 01:02:55two conditions this condition and
- 01:02:58this condition and both of those have to
- 01:03:01be true in order for it to return
- 01:03:03something right so if we save this and
- 01:03:05run it
- 01:03:06we see
- 01:03:07boom just mary brown number three right
- 01:03:11we can also use or
- 01:03:13instead we could go or in this case it's
- 01:03:17going to return two different results
- 01:03:20mary and wes why
- 01:03:22well because this is a different logical
- 01:03:25type of thing before we just wanted we
- 01:03:28wanted and and means both
- 01:03:30or
- 01:03:31means either or or right do this or do
- 01:03:35that so
- 01:03:37this has to be true
- 01:03:39or
- 01:03:40this has to be true and in our case up
- 01:03:43here
- 01:03:44the last name has to start with br
- 01:03:47or
- 01:03:48the number is three well this one starts
- 01:03:50with br its number is not three but it
- 01:03:53doesn't both have to be only one has to
- 01:03:55be true in order for it to return and in
- 01:03:57this case br
- 01:03:59is true for this one so it returns wes
- 01:04:02and mary so
- 01:04:04a
- 01:04:04pretty simple concept but very useful
- 01:04:07there are times when you need to search
- 01:04:08for several different things and i've
- 01:04:10just done
- 01:04:12two things
- 01:04:14one
- 01:04:15two you could do as many as you want
- 01:04:16just keep slapping on
- 01:04:19uh ors or
- 01:04:21or
- 01:04:24you know you get it or
- 01:04:25you know email
- 01:04:27is like
- 01:04:29something or you know and you could just
- 01:04:31keep going and going and going i don't
- 01:04:32know why you would want to but there are
- 01:04:33certain circumstances where you might
- 01:04:35have a few things you want to search for
- 01:04:37you could do that with a work if you
- 01:04:39need all of them to be true use and
- 01:04:42and
- 01:04:43pretty simple in the next video we'll
- 01:04:45look at limit
- 01:04:50okay in the last video we looked at and
- 01:04:52or in this video i will want to look at
- 01:04:54limiting so
- 01:04:56our little database only has five
- 01:04:58records so you know we likely would
- 01:05:01never want to limit the results but in
- 01:05:03reality you might have
- 01:05:05a thousand records ten thousand hundred
- 01:05:07thousand a million records two million
- 01:05:09records
- 01:05:11you know you might only wanna return a
- 01:05:13certain number
- 01:05:14of results right so to do that you use
- 01:05:18limit and it's very simple you just type
- 01:05:20in
- 01:05:21limit
- 01:05:22and then the number limit two right so
- 01:05:25here if we save this we're selecting the
- 01:05:27row id and everything from customers
- 01:05:30limit two
- 01:05:32i
- 01:05:33can't imagine you would guess what the
- 01:05:35result will be for this but
- 01:05:37it limits it to two results right
- 01:05:40so
- 01:05:41we have one and two
- 01:05:43let's see if we can string along some
- 01:05:46let's go uh descending this is going to
- 01:05:48throw an error i think
- 01:05:51i'm not sure so i'm going to run it yeah
- 01:05:53so we got to go you know
- 01:05:56order by
- 01:05:59what row
- 01:06:02id descending right that might work
- 01:06:09no
- 01:06:12we actually have to put our limit at the
- 01:06:14end in this case
- 01:06:17from customers order by row id
- 01:06:20descending limit to
- 01:06:22i think this will work now
- 01:06:24getting sloppy so now we have five and
- 01:06:26four the last two right
- 01:06:28um
- 01:06:29yeah
- 01:06:31i should mention all of these things
- 01:06:32we're learning you can usually just
- 01:06:34string along just by adding them to your
- 01:06:37executing statement here right
- 01:06:40so uh sometimes they have to be in
- 01:06:41specific order like we just saw just now
- 01:06:45order by has to go first and then limit
- 01:06:47it at the end
- 01:06:49if it doesn't work when you try
- 01:06:50something try changing the order and it
- 01:06:52probably will so
- 01:06:54you know
- 01:06:55pretty simple
- 01:06:57limit
- 01:06:58three limit however many you want
- 01:07:02and very cool so
- 01:07:03um that's limit it's very simple
- 01:07:06not a whole lot to it but i thought i
- 01:07:08would mention it just in case you ever
- 01:07:10need the limit and uh yeah so in the
- 01:07:13next video i'll show you how to delete a
- 01:07:15table
- 01:07:21okay so we've already looked at deleting
- 01:07:23a record that's pretty straightforward
- 01:07:25in this video i want to show you how to
- 01:07:26delete an entire table so like i said a
- 01:07:30couple videos ago when we talked about
- 01:07:31deleting when you remove stuff from a
- 01:07:33table you're dropping it from the table
- 01:07:36from the database same thing with a
- 01:07:38table you drop the table from the
- 01:07:41database right so
- 01:07:43to do that we want to c dot execute
- 01:07:49right and we're going to need to
- 01:07:52commit that
- 01:07:55i'll just go ahead and put that right
- 01:07:56there
- 01:07:57now inside of here
- 01:08:00first you take a a deep breath and then
- 01:08:04you just type in drop table
- 01:08:06and then
- 01:08:08the name of the table customers
- 01:08:10all right so if we save this
- 01:08:12it will drop the table and then try and
- 01:08:18output everything from the table which
- 01:08:20there shouldn't be anything in the table
- 01:08:22so
- 01:08:23see how that goes
- 01:08:26and we get an error no such table
- 01:08:28customers
- 01:08:30so that tells us that the
- 01:08:34the table has been dropped it has been
- 01:08:36deleted and like everything else
- 01:08:39when you delete a thing it's gone that
- 01:08:41table is gone it no longer exists
- 01:08:44and uh
- 01:08:46that's it so drop table
- 01:08:48pretty simple
- 01:08:50and uh
- 01:08:51yeah that's how that works so i'm gonna
- 01:08:53rebuild the table after this video and
- 01:08:55we're gonna play around with this stuff
- 01:08:56some more and sort of build a very basic
- 01:08:59uh database app that we can use and
- 01:09:02we'll start to look at that in the next
- 01:09:03video
- 01:09:08okay in the last video we deleted our
- 01:09:10table we dropped it i've gone ahead and
- 01:09:11recreated it i put all the data back in
- 01:09:13exactly as it was so we can continue to
- 01:09:15use it
- 01:09:16in this video i want to start to build
- 01:09:18out a little app and it takes sort of
- 01:09:20all the stuff we've learned up until now
- 01:09:22and sort of uses it so we've at this
- 01:09:25point we've learned all the basics for
- 01:09:28sqlite three with python and you know we
- 01:09:31went through it really quickly but
- 01:09:32there's not a whole lot to it's a very
- 01:09:33simple database to use and uh it's one
- 01:09:36of the nice things about it so
- 01:09:38from here on out we're not going to do
- 01:09:40anything revolutionary we're just going
- 01:09:41to build a simple app that sort of makes
- 01:09:44all the stuff we've done up until now a
- 01:09:45little easier sort of
- 01:09:48splits it apart into a couple of
- 01:09:49different files so it's easier to read
- 01:09:52and should be a lot of fun it'll help us
- 01:09:53reinforce the stuff we've already
- 01:09:55learned and
- 01:09:56i don't know just be kind of fun so i'm
- 01:09:58going to start right off the bat i'm
- 01:09:59going to create a new file and let's
- 01:10:01save this as
- 01:10:03i don't know our app
- 01:10:06dot pi
- 01:10:10and the first thing we want to do is
- 01:10:12import
- 01:10:14database and that's just
- 01:10:17this file right here that we've been
- 01:10:18working on throughout the whole course
- 01:10:20database.pi we can import that into
- 01:10:22another file
- 01:10:24so what we want to do here is
- 01:10:26take all of this stuff and
- 01:10:29build it into little functions python
- 01:10:31functions that we can then call
- 01:10:33from the other file so we'll sort of
- 01:10:36abstract all the database stuff away
- 01:10:39and
- 01:10:40just make everything a lot cleaner so
- 01:10:41i'm going to start out right here i'm
- 01:10:44just going to push all this stuff down
- 01:10:45let's create a function
- 01:10:47and let's go define and let's call
- 01:10:50let's call it show
- 01:10:52all
- 01:10:53right and what we want to do here is
- 01:10:55just show all the stuff in our database
- 01:10:58right in our table so i'm going to take
- 01:11:00all of this stuff
- 01:11:03and i'm just going to paste it inside
- 01:11:06of this function
- 01:11:10and
- 01:11:13paste
- 01:11:14and same thing
- 01:11:17with this
- 01:11:22and
- 01:11:24let's add these guys
- 01:11:28as well
- 01:11:33indenting is important for functions so
- 01:11:38okay and so we can get rid of that
- 01:11:42now
- 01:11:43these two guys i think we need to put
- 01:11:46these in
- 01:11:48each of our functions
- 01:11:51that we create
- 01:11:56okay
- 01:11:58so
- 01:12:00i think that looks good right
- 01:12:06so let's create a comment and let's call
- 01:12:08uh queer let's go query
- 01:12:11quest query
- 01:12:13the
- 01:12:14database
- 01:12:15and
- 01:12:16return
- 01:12:18all
- 01:12:19records
- 01:12:21right so
- 01:12:23here we're connecting to the database
- 01:12:25creating a cursor executing a command to
- 01:12:28show all the stuff in there
- 01:12:31putting it into this variable and then
- 01:12:33looping through that variable and
- 01:12:34printing out each item onto the screen
- 01:12:36and then closing
- 01:12:38making our commit in this case we don't
- 01:12:39actually need to commit anything because
- 01:12:41we're not we're just
- 01:12:42querying we're not committing
- 01:12:44uh so we'll probably take that out
- 01:12:46eventually
- 01:12:47and then at the end we want to close our
- 01:12:48connection so if we save this
- 01:12:50and head back over here
- 01:12:52now if we want to actually use that we
- 01:12:56just
- 01:12:57call
- 01:12:58database dot
- 01:13:00show underscore all
- 01:13:03like you would call any function
- 01:13:05we need to
- 01:13:06put it in this database because the name
- 01:13:08of the file here is database dot
- 01:13:10show all which is the name of that
- 01:13:12function right there
- 01:13:14and we're not passing anything in
- 01:13:15because it doesn't require us to all
- 01:13:18right so if we save this what do we call
- 01:13:19this our app.pi
- 01:13:21and if we head back over here we can go
- 01:13:23python
- 01:13:26our
- 01:13:29app.pi
- 01:13:30if we run it boom
- 01:13:32it prints out everything
- 01:13:35onto the screen very very cool
- 01:13:38now likewise if we commented this out
- 01:13:41and saved this and ran it
- 01:13:44again not that's going to happen
- 01:13:46so
- 01:13:47there's nothing else going on in our r
- 01:13:49app dot pi
- 01:13:51so
- 01:13:52and our database dot pi only has one
- 01:13:54function now we're going to spend the
- 01:13:56rest of the course building out more
- 01:13:57functions that we can then call but
- 01:14:00that's a good way to get started i think
- 01:14:02and the next video will go on and do the
- 01:14:05next thing
- 01:14:10okay so we've created a function to show
- 01:14:12everything now let's create a function
- 01:14:14to add a record
- 01:14:16to our database and so let's call this
- 01:14:18define
- 01:14:19and let's call add one
- 01:14:22right
- 01:14:25and here let's make a comment that says
- 01:14:28add
- 01:14:29a new record to the
- 01:14:32table
- 01:14:34all right
- 01:14:35so we need to pass in
- 01:14:37the stuff that we want to add to the
- 01:14:39table so
- 01:14:41we want first let's call it last
- 01:14:44and email
- 01:14:46right
- 01:14:47so
- 01:14:48we already know
- 01:14:50how to
- 01:14:51add
- 01:14:54stuff to our database what we do is well
- 01:14:56first we need
- 01:14:58these two guys
- 01:15:02we need to create a connection and a
- 01:15:04cursor and since these are inside
- 01:15:06functions you need to do it every time
- 01:15:09connect to the database
- 01:15:10and create cursor let's just get rid of
- 01:15:14this
- 01:15:15there we go
- 01:15:17right
- 01:15:18next we go c dot execute as we already
- 01:15:21know how to do
- 01:15:23and we want to insert
- 01:15:25into and our
- 01:15:27table is customers
- 01:15:30and the values
- 01:15:33are
- 01:15:34we want to put placeholders we learned
- 01:15:36how to do that already
- 01:15:39right
- 01:15:40now
- 01:15:42to add the variables that we passed
- 01:15:45first last in email
- 01:15:48we just put them right here
- 01:15:50first last email
- 01:15:54okay and then we need to commit this
- 01:15:59and close our connection
- 01:16:02right underneath there boom
- 01:16:05commit our command
- 01:16:06close our connection
- 01:16:08all right that looks pretty good
- 01:16:11so if we save this
- 01:16:13and head back over to our app
- 01:16:16now
- 01:16:17to run this guy we just called database
- 01:16:20dot
- 01:16:22add one
- 01:16:24do we call it add one let's make sure
- 01:16:25here
- 01:16:27add one yep now we need to pass it three
- 01:16:30things a first name a last name and an
- 01:16:31email
- 01:16:32so let's go um
- 01:16:35oops wrong one
- 01:16:37here let's go
- 01:16:39well
- 01:16:41first name last name and email
- 01:16:44we might go who
- 01:16:46um give me a name here
- 01:16:49uh laura
- 01:16:52smith
- 01:16:54lara at
- 01:16:56smith.com
- 01:16:58right
- 01:16:59so this will run that and then if we
- 01:17:01want to show it all we'll keep this one
- 01:17:03here
- 01:17:05just to
- 01:17:08to make sure so let's go
- 01:17:10add
- 01:17:11a record
- 01:17:13to the database
- 01:17:16here let's go show all the records
- 01:17:20all right so if we save this and run it
- 01:17:26boom we see laura smith laura at
- 01:17:28smith.com
- 01:17:30has been added
- 01:17:32very cool very easy
- 01:17:34and uh that's all there is to it in the
- 01:17:36next video we will create a function to
- 01:17:38delete a record from our database
- 01:17:45okay in the last video we added a new
- 01:17:46record to the table in this video we
- 01:17:48want to create a function that deletes a
- 01:17:50record from the table and this was a
- 01:17:51little bit trickier than you might
- 01:17:53expect and i'll show you what i'm
- 01:17:55talking about in just a second so let's
- 01:17:56head over here to our database.pi file
- 01:17:59and let's create a
- 01:18:02delete
- 01:18:03one
- 01:18:07function and we want to pass in id
- 01:18:10now inside of here
- 01:18:12again we're gonna
- 01:18:13make our connections here
- 01:18:16and we want to close our connections
- 01:18:18here
- 01:18:21and inside of here
- 01:18:23we want to c dot execute
- 01:18:27all right and we know how to do this
- 01:18:28already we want to
- 01:18:31delete
- 01:18:32delete from customers
- 01:18:36where
- 01:18:38and we want to let's do row id
- 01:18:41equals and here we want to put
- 01:18:44a placeholder
- 01:18:47all right and then
- 01:18:49we want to pass in this id
- 01:18:52which is this id right
- 01:18:55so
- 01:18:55that looks pretty good
- 01:19:00okay so that'll work but now head back
- 01:19:03over to our app to use this thing we
- 01:19:06call database dot what do we call this
- 01:19:08remove
- 01:19:10delete one
- 01:19:14let's copy this dot delete one and now
- 01:19:18we want to pass in the number of the id
- 01:19:20we want to delete in this case we want
- 01:19:21to delete 6.
- 01:19:23it's not up anymore but 6 was the one we
- 01:19:25just added
- 01:19:27in the last video now here's the weird
- 01:19:29thing
- 01:19:30the ids are numbers right integers we
- 01:19:34know they're integers the database knows
- 01:19:36they're integers so you would think we
- 01:19:39would pass an integer but if we do
- 01:19:41we save this and run it
- 01:19:45we get an error
- 01:19:47it says module database has no attribute
- 01:19:49delete one wait that's the wrong error
- 01:19:52uh database delete one did we not save
- 01:19:56let's pull this back up
- 01:19:59delete one
- 01:20:00must not have saved it all right so
- 01:20:02now let's clear the screen run this
- 01:20:05again
- 01:20:06all right this is the different this is
- 01:20:07the the error i thought we would get
- 01:20:09value error parameters are of
- 01:20:11unsupported type for this id
- 01:20:14right we're passing a number an integer
- 01:20:17for some reason we can't do that we need
- 01:20:19to pass
- 01:20:21this six
- 01:20:23as a string
- 01:20:25so let's go delete
- 01:20:28record
- 01:20:30use row id as string right
- 01:20:36so now if we save this in quotation
- 01:20:38marks it'll pass this six
- 01:20:40over
- 01:20:41to here id becomes six a string that
- 01:20:45gets put into there that then somehow
- 01:20:47magically becomes a number again and it
- 01:20:49should work it's the weirdest thing
- 01:20:53if we run it boom now it works six has
- 01:20:56been deleted
- 01:20:58and just that simple so
- 01:21:00that's how you delete stuff
- 01:21:02in the next video i think we already
- 01:21:04looked at how to add a one record let's
- 01:21:08create one to add a bunch of records all
- 01:21:10at once and we'll look at that in the
- 01:21:12next video
- 01:21:17okay so in this video we want to create
- 01:21:19a function to add many records to our
- 01:21:22table we've already done one where we've
- 01:21:24add one record
- 01:21:26so make sense that we would do for many
- 01:21:28so let's go define
- 01:21:30add many
- 01:21:32and let's pass in a list we'll just call
- 01:21:34it list
- 01:21:36and
- 01:21:37we can i'm just going to copy all of
- 01:21:39this stuff
- 01:21:43so
- 01:21:45what we want to do is execute many
- 01:21:48and then insert into customers values
- 01:21:52then here
- 01:21:53we want
- 01:21:55to pass in list which is this guy right
- 01:21:58here
- 01:21:59all right
- 01:22:00does that look good
- 01:22:02i think so so let's save this and head
- 01:22:04back over here
- 01:22:06and we don't want to delete anything so
- 01:22:08here let's go
- 01:22:10add
- 01:22:13many records
- 01:22:17all right so we want to call
- 01:22:19database dot
- 01:22:23add many
- 01:22:26and we want to pass in
- 01:22:28our
- 01:22:29stuff i don't know
- 01:22:31so here we need to define stuff we need
- 01:22:34to create a list
- 01:22:36right
- 01:22:37and
- 01:22:39inside of this
- 01:22:41let's add let's add two records
- 01:22:43all right so here we want
- 01:22:47bump
- 01:22:49bump
- 01:22:51well
- 01:22:52and i'm just going to copy this
- 01:22:56and here let's go brenda
- 01:23:01um
- 01:23:02smitherton
- 01:23:03i don't know
- 01:23:04and she is brenda
- 01:23:07smitherton.com
- 01:23:11and here we'll call joshua
- 01:23:15raintree
- 01:23:17i don't know where i get these names
- 01:23:18josh at
- 01:23:23raintree.com all right so we're going to
- 01:23:26call add many which is
- 01:23:28this guy
- 01:23:30we're going to pass in stuff which is
- 01:23:32all of this
- 01:23:36right
- 01:23:39so stuff which is this list
- 01:23:41becomes
- 01:23:43list
- 01:23:44and then here we just pass that in
- 01:23:47and since we have execute many it should
- 01:23:49work
- 01:23:51i think
- 01:23:52fingers crossed
- 01:23:55okay and then at the end we'll show all
- 01:23:57so all right let's see if this works
- 01:23:59let's clear the screen
- 01:24:02boom
- 01:24:04so we have brenda smitherton and joshua
- 01:24:07raintree number six and number seven
- 01:24:11and it worked just that easy all right
- 01:24:13so that was pretty cool a little bit
- 01:24:15more complicated but
- 01:24:17still not too bad now
- 01:24:20we've sort of done all this stuff in
- 01:24:22this program here and which i think you
- 01:24:25would probably want to do as opposed to
- 01:24:27adding it here somehow
- 01:24:29so that makes sense right
- 01:24:31yeah
- 01:24:32okay so that's all for this video in the
- 01:24:34next video we'll look at
- 01:24:37where
- 01:24:38and uh
- 01:24:39yeah i think that's about
- 01:24:41getting to the end of the stuff that we
- 01:24:42need to add into your as functions so
- 01:24:45yeah look at that in the next video
- 01:24:51okay so let's crank out one more of
- 01:24:53these functions let's look at the where
- 01:24:55clause now
- 01:24:57to create a function to deal with where
- 01:24:59is kind of silly i mean there's just so
- 01:25:01many options you could
- 01:25:03look up you know pull all the
- 01:25:05records from a database where first name
- 01:25:08is x or where last name is x or where
- 01:25:10email address is x or where
- 01:25:12you know row id is x or you know any of
- 01:25:16a zillion different permutations of that
- 01:25:18so it doesn't really make a lot of sense
- 01:25:20to create a where function
- 01:25:22but we're not really building a real app
- 01:25:24here we're just sort of playing around
- 01:25:26with this to reinforce some of the
- 01:25:27concepts so we might as well just knock
- 01:25:29one out and
- 01:25:31let's just say we want to look up email
- 01:25:33addresses it's pretty simple so
- 01:25:36let's just call
- 01:25:38define email
- 01:25:40look up
- 01:25:43and we want to pass email i guess
- 01:25:46all right so i'm just gonna copy all of
- 01:25:48this
- 01:25:51paste it in
- 01:25:53and
- 01:25:54for our execute
- 01:25:56what do we want to do we want to select
- 01:25:59everything from customers right
- 01:26:03where
- 01:26:04and we want where email
- 01:26:06equals
- 01:26:08and we want to pass in
- 01:26:12email
- 01:26:15all right
- 01:26:16now this is a tuple we're passing one
- 01:26:18thing
- 01:26:19and so
- 01:26:21we put a little
- 01:26:22comma afterwards just a little quirk
- 01:26:25and uh
- 01:26:26sort of how that goes now we also
- 01:26:29we're not committing anything here we're
- 01:26:32querying and we want to put it up on the
- 01:26:34screen so we need to come up here
- 01:26:36and let's see
- 01:26:40like a with a query
- 01:26:41we need to
- 01:26:43fetch all
- 01:26:44and let's loop through
- 01:26:46and print those out so i'm just going to
- 01:26:47copy that
- 01:26:50and that looks
- 01:26:51good i think
- 01:26:55yeah
- 01:26:57okay so i think that looks good right
- 01:27:00let's save this and head back over to
- 01:27:02our app and look up email address so we
- 01:27:04want to call database
- 01:27:06dot email
- 01:27:08lookup
- 01:27:10and we want to pass something so let's
- 01:27:12pass john codamy.com we know that's an
- 01:27:16address that exists in our system
- 01:27:18so we can head back over here
- 01:27:21run this again oh first of all
- 01:27:25let's comment out this
- 01:27:27show all one we don't need to show all
- 01:27:29we just want to show
- 01:27:31the one that we're looking up so save
- 01:27:32this
- 01:27:35run it
- 01:27:36oh
- 01:27:38oh damn data nace
- 01:27:41why didn't you tell me i misspelled that
- 01:27:44database all right
- 01:27:46let's clear this screen and run this
- 01:27:48again and boom john elder
- 01:27:51john codamy.com if we want the id
- 01:27:55as well
- 01:27:56so we would go just we could add it like
- 01:27:58this row id and everything
- 01:28:01come back here
- 01:28:03and now we get the row id so you know
- 01:28:05you can do this for anything you want if
- 01:28:07you want to
- 01:28:08create an email lookup one you could do
- 01:28:10that if you wanted to create a specific
- 01:28:12row id you know primary key id lookup
- 01:28:15you could do that like i said this app
- 01:28:18we're building here
- 01:28:19we have built i suppose
- 01:28:22is
- 01:28:23you know this is not supposed to be some
- 01:28:24professional app we're building this is
- 01:28:26just a little toy thing that we can sort
- 01:28:28of play around with just to reinforce
- 01:28:30all of these different things and it's
- 01:28:32kind of nice to have this you know
- 01:28:35examples of how to for instance query
- 01:28:39you know right there how to add
- 01:28:41something to a table you know right
- 01:28:43there how to add many things to a table
- 01:28:46right there how to delete something from
- 01:28:47a table it's just kind of nice to have
- 01:28:49them all in one file
- 01:28:50and
- 01:28:52you know
- 01:28:55for reference going forward into the
- 01:28:57future if you need to refer back to this
- 01:28:58if you've built this with with me along
- 01:29:00with me you'll have this to sort of you
- 01:29:02know reference back so it's kind of cool
- 01:29:04so i think that's all for this video in
- 01:29:06the next video we'll say a few more
- 01:29:08things and wrap this all up okay so
- 01:29:09that's all for this video and for the
- 01:29:11course if you liked it be sure to smash
- 01:29:13the like button below subscribe to the
- 01:29:14channel give me a thumbs up for the
- 01:29:16youtube algorithm and check out my
- 01:29:17website kodamy.com where you can use
- 01:29:19coupon code camp to get 65 off
- 01:29:21membership so you pay just 27 to access
- 01:29:23all my courses over 40 courses hundreds
- 01:29:26of videos in the pdfs of all my
- 01:29:27best-selling coding books join over a
- 01:29:29hundred thousand students learning to
- 01:29:30code just like you my name is john elder
- 01:29:32from codep.com and we'll see in the next
- 01:29:34video
- coding
- Python
- SQLite
- text editor
- software setup
- Git Bash
- programming tools
- course introduction
- John Elder
- codemy.com