SQLite Databases With Python - Full Course

01:29:37
https://www.youtube.com/watch?v=byHcYRpMgI4

الملخص

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.

الوجبات الجاهزة

  • 👨‍🏫 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.

الجدول الزمني

  • 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

الأسئلة الشائعة

  • 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.

عرض المزيد من ملخصات الفيديو

احصل على وصول فوري إلى ملخصات فيديو YouTube المجانية المدعومة بالذكاء الاصطناعي!
الترجمات
en
التمرير التلقائي:
  • 00:00:00
    all right welcome to the course my name
  • 00:00:01
    is john elder from cody.com and i'll be
  • 00:00:03
    your instructor today in this video i
  • 00:00:05
    want to spend just a couple of minutes
  • 00:00:06
    sort of touching base talking a little
  • 00:00:08
    bit about what to expect from the course
  • 00:00:10
    talking about some of the tools that
  • 00:00:11
    we're going to need and letting you know
  • 00:00:13
    how to get in contact with me if you
  • 00:00:14
    have any questions along the way but
  • 00:00:16
    before we get started if you like this
  • 00:00:17
    video you want to see more like it be
  • 00:00:19
    sure to smash the like button below
  • 00:00:20
    subscribe to that channel give me a
  • 00:00:21
    thumbs up for the youtube algorithm and
  • 00:00:23
    check out my website codeme.com where i
  • 00:00:25
    have dozens of courses with hundreds of
  • 00:00:26
    videos that teach you to code use coupon
  • 00:00:28
    code camp to get 65 off membership
  • 00:00:30
    that's all my courses videos and books
  • 00:00:32
    for one-time fee at just 27 which is
  • 00:00:34
    insanely cheap so right off the bat i'm
  • 00:00:36
    on a windows computer hopefully you are
  • 00:00:38
    too if you're on mac or linux though no
  • 00:00:40
    big deal whatsoever you can definitely
  • 00:00:41
    follow along all the tools and things
  • 00:00:43
    that we're going to use in this course
  • 00:00:44
    have a mac or linux equivalent and there
  • 00:00:47
    shouldn't be any real changes at all
  • 00:00:49
    between what i'm doing here and what
  • 00:00:50
    you'll do on your mac or linux
  • 00:00:52
    the code is going to be the exact same
  • 00:00:54
    there's no changes that need to be made
  • 00:00:56
    or anything like that it should work
  • 00:00:57
    just fine so speaking of the tools that
  • 00:00:59
    we're going to need we're going to need
  • 00:01:00
    obviously python now sqlite actually
  • 00:01:03
    comes with python so there's nothing
  • 00:01:05
    else to download besides python it's
  • 00:01:07
    we'll also need some sort of text editor
  • 00:01:09
    to write our code on now i'm going to
  • 00:01:10
    use something called sublime text if you
  • 00:01:12
    have some other text editor that you
  • 00:01:14
    like using pycharm visual studio code
  • 00:01:17
    anything at all notepad plus plus
  • 00:01:20
    absolutely feel free to use that if
  • 00:01:21
    you're comfortable using that if you
  • 00:01:22
    know how to use that if you know if you
  • 00:01:25
    like using a particular one over another
  • 00:01:27
    one absolutely feel free to do that no
  • 00:01:29
    big deal whatsoever we also need some
  • 00:01:31
    sort of terminal to write our commands
  • 00:01:33
    on now windows computers come with
  • 00:01:34
    several terminals there's the command
  • 00:01:35
    prompt there's powershell now those
  • 00:01:38
    aren't that great so i'm going to
  • 00:01:39
    download something else something called
  • 00:01:41
    the get bash terminal it's completely
  • 00:01:42
    free sublime text is free too as well as
  • 00:01:45
    python so nothing to buy in order to
  • 00:01:47
    watch this course
  • 00:01:49
    but like i said get bash works great so
  • 00:01:50
    we're going to use that if you're on mac
  • 00:01:52
    or linux you could just use the terminal
  • 00:01:54
    that comes with mac or linux just go to
  • 00:01:55
    the little search function type in
  • 00:01:57
    terminal and you should be fine
  • 00:01:59
    you can absolutely download the git bash
  • 00:02:01
    terminal on mac or linux if you want to
  • 00:02:03
    follow along exactly but it's definitely
  • 00:02:05
    not necessary so
  • 00:02:07
    that's really all the tools we're going
  • 00:02:08
    to need in order to take this course
  • 00:02:10
    like i said not much to sqlite as far as
  • 00:02:13
    tools and stuff
  • 00:02:15
    since it just comes with python so
  • 00:02:16
    that's really cool so finally i want to
  • 00:02:18
    talk about how to get in contact me if
  • 00:02:20
    you have questions so i'm in las vegas
  • 00:02:22
    which is pacific west coast time in the
  • 00:02:24
    u.s so if you're on the other side of
  • 00:02:26
    the world and it's during the day i'm
  • 00:02:28
    probably sleeping back in vegas so just
  • 00:02:29
    sort of keep that in mind when you sort
  • 00:02:31
    of factor in response time but
  • 00:02:33
    absolutely feel free to ask questions
  • 00:02:35
    along the way if you don't understand
  • 00:02:36
    something if your code's not working
  • 00:02:38
    correctly if you just don't quite get
  • 00:02:40
    something that i'm saying and you need
  • 00:02:42
    some clarification shoot me a message
  • 00:02:44
    that's what i'm here for i do this full
  • 00:02:46
    time this is my job so i'm either
  • 00:02:48
    creating courses or answering questions
  • 00:02:50
    for students and so that's cool you can
  • 00:02:52
    get in contact with me on my website
  • 00:02:54
    kodamy.com there's a contact form you
  • 00:02:56
    just click on it it shoots straight to
  • 00:02:58
    my email so i'll see that as soon as i
  • 00:03:00
    you know as soon as my email dings and i
  • 00:03:02
    can see it you can also obviously post a
  • 00:03:05
    comment below this video if you're
  • 00:03:07
    watching this on youtube or wherever
  • 00:03:09
    and that will work just fine too so
  • 00:03:13
    there are no stupid questions if you
  • 00:03:14
    know if you want to ask a question on
  • 00:03:16
    something else i'm not as receptive but
  • 00:03:19
    absolutely anything in this course
  • 00:03:21
    anything in these videos you don't quite
  • 00:03:22
    understand or like i said if your code
  • 00:03:24
    isn't working if you're getting errors
  • 00:03:25
    or something shoot me a message we'll
  • 00:03:27
    get to the bottom of it and that's cool
  • 00:03:33
    all right first things first we need
  • 00:03:34
    python so chances are you might already
  • 00:03:37
    have python on your computer even if you
  • 00:03:39
    do i recommend you uninstall it
  • 00:03:42
    reinstall it and download it again and
  • 00:03:44
    reinstall it and i'll show you why in
  • 00:03:46
    just a second so to do that
  • 00:03:48
    we're going to head over to
  • 00:03:50
    python.org you could google it if you
  • 00:03:52
    want or just type in python.org and come
  • 00:03:54
    to downloads and you can see right here
  • 00:03:56
    says
  • 00:03:57
    python3.7.3 that's the most current
  • 00:03:59
    version right now if that version has
  • 00:04:01
    changed by the time you watch this video
  • 00:04:03
    as long as it's not version 4 anything
  • 00:04:05
    below 4 go ahead and download whatever
  • 00:04:07
    version it is perfectly fine so i'm
  • 00:04:10
    gonna go ahead and click this button
  • 00:04:12
    and we want to save this anywhere i'm
  • 00:04:13
    just going to save it on my desktop
  • 00:04:15
    and it's not a very big file in fact
  • 00:04:18
    it's boom it already downloaded and so
  • 00:04:21
    go ahead and click it
  • 00:04:22
    to install this thing
  • 00:04:24
    and we want to run it
  • 00:04:27
    oops clicked it a couple of times there
  • 00:04:29
    okay so
  • 00:04:31
    install python 3.72 32-bit now this is
  • 00:04:33
    the first thing there is a 64-bit
  • 00:04:35
    version the 32-bit version is actually
  • 00:04:38
    the one that you want it's the most
  • 00:04:39
    common one uh it works perfectly fine on
  • 00:04:41
    64-bit computers we all have a 64-bit
  • 00:04:44
    computer so uh
  • 00:04:46
    don't try and go find the 64-bit you can
  • 00:04:49
    download the 64-bit version from the
  • 00:04:51
    website i don't recommend that you do
  • 00:04:52
    that it's just a hassle there's no real
  • 00:04:55
    benefit to doing that
  • 00:04:56
    so this is the one we want now before we
  • 00:04:58
    click this button this is the most
  • 00:05:00
    important thing right here
  • 00:05:02
    and
  • 00:05:03
    it is so very important and this is why
  • 00:05:05
    i recommend that you download this and
  • 00:05:07
    reinstall it even if python is already
  • 00:05:09
    on your computer
  • 00:05:10
    and that's this little check box right
  • 00:05:11
    here add python 3.7 to path and on a
  • 00:05:14
    windows computer path means
  • 00:05:17
    uh you can run python in this case
  • 00:05:19
    anywhere on your computer right you
  • 00:05:21
    don't have to be in the python directory
  • 00:05:24
    in order to run python if you don't
  • 00:05:26
    check this box you have to be in the
  • 00:05:28
    python directory in order to run python
  • 00:05:30
    you can't just run it from anywhere so
  • 00:05:32
    we want to be able to run it from
  • 00:05:33
    anywhere right so go ahead and click
  • 00:05:36
    that box make sure there's a little
  • 00:05:37
    check box the check mark is is selected
  • 00:05:40
    on there and it's so weird that by
  • 00:05:42
    default this is not selected we
  • 00:05:44
    absolutely want this checked
  • 00:05:46
    and if you installed python some other
  • 00:05:48
    time chances are you didn't click that
  • 00:05:50
    little box because it's not the default
  • 00:05:53
    you're not paying attention whoever
  • 00:05:54
    reads the stuff on the installation
  • 00:05:56
    screen you just click next right so
  • 00:05:58
    there's a good chance that you don't
  • 00:06:00
    have python installed on your path and
  • 00:06:01
    we want to make sure that it is so go
  • 00:06:03
    ahead and check that
  • 00:06:04
    once you do that go ahead and click
  • 00:06:06
    install now
  • 00:06:07
    and i'm going to click no and cancel
  • 00:06:09
    this thing because i've already
  • 00:06:11
    installed python but you'll click ok and
  • 00:06:14
    just wait for the installation process
  • 00:06:16
    to complete it'll scroll through some
  • 00:06:18
    stuff there'll be a little bar status
  • 00:06:20
    bar and then that'll be that and you're
  • 00:06:22
    good to go now in order to use python we
  • 00:06:24
    don't have to launch python we don't
  • 00:06:26
    have to start it we don't have to do
  • 00:06:27
    anything at all we've added it to our
  • 00:06:29
    path now we can just use it anytime we
  • 00:06:31
    want anywhere we want always on our
  • 00:06:34
    computer so very cool if you're on a mac
  • 00:06:36
    i don't have instructions for you to
  • 00:06:39
    install on a mac you can just go to
  • 00:06:40
    youtube and type in python installation
  • 00:06:43
    mac and you'll see a little two-minute
  • 00:06:44
    video that'll show you if you don't
  • 00:06:46
    already know yourself chances are you
  • 00:06:48
    already do
  • 00:06:49
    so we'll just leave it at that so that's
  • 00:06:51
    all for this video in the next video
  • 00:06:52
    we'll jump in and look at installing our
  • 00:06:54
    text editor and our command prompt
  • 00:06:56
    terminal
  • 00:07:02
    so we've installed python now we want to
  • 00:07:03
    install the text editor that we're going
  • 00:07:06
    to use throughout this course and
  • 00:07:09
    i am just using this it's the sublime
  • 00:07:12
    text editor we'll take a look at it more
  • 00:07:14
    detail in just a minute but it's sort of
  • 00:07:16
    like the standard text editor most
  • 00:07:18
    coders use it it's super easy to use it
  • 00:07:20
    has all the bells and whistles that you
  • 00:07:22
    would want and it's completely free now
  • 00:07:24
    uh you can actually pay a little box
  • 00:07:26
    will pop up every once in a while asking
  • 00:07:28
    for a donation so if you want to throw
  • 00:07:30
    those guys some money that's cool you
  • 00:07:32
    absolutely don't have to you can use it
  • 00:07:34
    forever for free just whenever that box
  • 00:07:36
    pops up every few weeks or so just click
  • 00:07:38
    no and that's that so to use this thing
  • 00:07:41
    i'm just going to go to google and type
  • 00:07:42
    in sublime text
  • 00:07:44
    and the first thing that popped up is
  • 00:07:46
    sublimetext.com
  • 00:07:48
    or you could just go directly there and
  • 00:07:50
    here's the website all you have to do is
  • 00:07:51
    click download for windows right now
  • 00:07:54
    it's sublime text
  • 00:07:56
    three
  • 00:07:57
    build 3207 if it's a different number by
  • 00:08:00
    the time you watch this video no big
  • 00:08:01
    deal whatsoever just download the latest
  • 00:08:03
    version now i'm not going to install
  • 00:08:04
    this because there's nothing to it uh
  • 00:08:07
    just like in the last video with python
  • 00:08:08
    the little thing will pop up down here
  • 00:08:10
    you click it and just walk through the
  • 00:08:13
    installation wizard i don't think
  • 00:08:14
    there's anything you actually have to do
  • 00:08:16
    i just
  • 00:08:17
    click all the defaults ok and then
  • 00:08:20
    install it and after you've installed it
  • 00:08:22
    you can run it by going to your windows
  • 00:08:24
    start menu and just typing in sublime
  • 00:08:25
    and the little icon will pop up and you
  • 00:08:27
    can click it so that's cool now the last
  • 00:08:30
    thing we need and the only other thing
  • 00:08:32
    we need is a terminal something to run
  • 00:08:35
    commands on
  • 00:08:36
    and
  • 00:08:38
    it really doesn't matter what kind of
  • 00:08:39
    terminal you use if you're on linux or
  • 00:08:41
    mac they come with a terminal that's
  • 00:08:42
    perfectly fine you can use that on a mac
  • 00:08:44
    just go up the little search thing at
  • 00:08:46
    the type top and type in terminal and
  • 00:08:48
    it'll pop up
  • 00:08:49
    you know if you're on windows there's a
  • 00:08:52
    command prompt or a power shell you can
  • 00:08:55
    probably use either of those i don't
  • 00:08:57
    like using those i enjoy instead using
  • 00:09:00
    the python or using the get bash
  • 00:09:02
    terminal it allows us to do version
  • 00:09:04
    control we're not going to do any
  • 00:09:06
    version control in this course because
  • 00:09:07
    there's not much to it
  • 00:09:08
    code wise but if you've got a big
  • 00:09:11
    project and you want to use git or
  • 00:09:13
    github you need something that allows
  • 00:09:15
    for that and this git bash terminal does
  • 00:09:17
    so it's really nice and it's absolutely
  • 00:09:19
    free so to use that i'm just going to go
  • 00:09:21
    to google google and type in git bash
  • 00:09:24
    that should be it and then here's the
  • 00:09:26
    website get dash scm dot com forward
  • 00:09:28
    slash downloads
  • 00:09:30
    and so we can click
  • 00:09:32
    download this and i will walk you
  • 00:09:33
    through this one because it's a little
  • 00:09:35
    weird so i'm just going to download this
  • 00:09:36
    to
  • 00:09:38
    the desktop
  • 00:09:39
    and
  • 00:09:40
    it takes a little bit longer for this to
  • 00:09:41
    download it's only 44 megabytes
  • 00:09:44
    so it should download immediately
  • 00:09:45
    especially on a high speed internet like
  • 00:09:47
    i have
  • 00:09:48
    for some reason it takes a bit to
  • 00:09:49
    download it always has
  • 00:09:51
    something about this website or
  • 00:09:52
    something it's just it's on a slow
  • 00:09:54
    server or something and here we go it's
  • 00:09:57
    downloaded so go ahead and click that
  • 00:09:59
    and run it
  • 00:10:03
    and let me pull this over
  • 00:10:05
    and so okay it's just the license click
  • 00:10:07
    next and you can leave all of these the
  • 00:10:09
    defaults
  • 00:10:11
    now here it's saying what text editor
  • 00:10:13
    would you like to use now this is weird
  • 00:10:15
    because we're not going to use this in a
  • 00:10:17
    text editor you don't you use it it's a
  • 00:10:19
    standalone thing right so
  • 00:10:21
    we are going to use sublime text so i'll
  • 00:10:23
    just leave this but you can just ignore
  • 00:10:25
    this if you want
  • 00:10:27
    just click next and here we want to use
  • 00:10:30
    git and optional unix tools from the
  • 00:10:32
    command prompt
  • 00:10:34
    we're gonna like i said we're also we're
  • 00:10:35
    gonna use the get batch terminal on its
  • 00:10:37
    own so this again doesn't really matter
  • 00:10:39
    but
  • 00:10:40
    we'll leave this one clicked here at the
  • 00:10:42
    bottom and click next
  • 00:10:44
    open ssh this is for uh pushing stuff up
  • 00:10:46
    to like github where you need an ssh key
  • 00:10:49
    we're not going to use any of this
  • 00:10:51
    doesn't matter we just click next same
  • 00:10:52
    for this click next
  • 00:10:54
    and this is a well this is a get thing
  • 00:10:56
    we're not going to be using git really
  • 00:10:58
    in this course
  • 00:11:00
    so we can click next again
  • 00:11:02
    same thing with this min tty thing click
  • 00:11:04
    next
  • 00:11:06
    basically we're just taking all of the
  • 00:11:07
    defaults again click next
  • 00:11:10
    and you get to this final thing and then
  • 00:11:12
    go ahead and click install now i'm not
  • 00:11:14
    going to do that because i've already
  • 00:11:15
    installed it so i'll just click cancel
  • 00:11:17
    but you can click install
  • 00:11:19
    am i sure yes i am and it takes a couple
  • 00:11:22
    of minutes to install it has a little
  • 00:11:24
    you know
  • 00:11:26
    status bar thing that'll scroll through
  • 00:11:28
    and finish when it's finished and uh so
  • 00:11:30
    yeah that's
  • 00:11:31
    all there is to it so
  • 00:11:33
    those are all the tools we need we don't
  • 00:11:34
    have to download anything else in order
  • 00:11:36
    to start using this so in the next video
  • 00:11:38
    we'll jump right in and start writing
  • 00:11:39
    some code and using the database
  • 00:11:46
    all right so we've got all the tools we
  • 00:11:48
    need all the things we have to download
  • 00:11:50
    and install now we can just start using
  • 00:11:52
    this database so
  • 00:11:54
    most people don't realize that sql light
  • 00:11:55
    3 comes with python it's built in
  • 00:11:58
    already so there's nothing to download
  • 00:11:59
    there's nothing to install it's already
  • 00:12:01
    there all you have to do is just sort of
  • 00:12:04
    reference it in your code and start
  • 00:12:06
    using it so
  • 00:12:08
    in order to use sqlite3 all we have to
  • 00:12:12
    do is import
  • 00:12:13
    and then name it so it's sql ite 3 right
  • 00:12:19
    so
  • 00:12:20
    this is just a blank file that we've
  • 00:12:22
    opened in sublime first thing we want to
  • 00:12:24
    do is save this and we need to save this
  • 00:12:26
    as a python file so i'm going to come up
  • 00:12:28
    here to file
  • 00:12:29
    and save as
  • 00:12:31
    and
  • 00:12:33
    go to our c directory here and we need
  • 00:12:35
    to create a new directory in order to
  • 00:12:37
    hold all the files that we're going to
  • 00:12:38
    be creating throughout this course so
  • 00:12:40
    i'm just going to right click and click
  • 00:12:41
    new folder and let's just name this
  • 00:12:44
    sqlite
  • 00:12:46
    so then we can click on here
  • 00:12:48
    and
  • 00:12:49
    we can name this anything we want
  • 00:12:51
    let's name it sqlite.pi
  • 00:12:53
    well no
  • 00:12:55
    we don't want to name it the same thing
  • 00:12:56
    that we
  • 00:12:58
    import here
  • 00:13:00
    sqlite3 so let's just call this a
  • 00:13:02
    database
  • 00:13:04
    dot pi
  • 00:13:06
    and you see as soon as we do that
  • 00:13:08
    things sort of change color a little bit
  • 00:13:11
    this becomes red that tells us that
  • 00:13:14
    sublime now knows that this is a python
  • 00:13:18
    program a python file that we've created
  • 00:13:20
    right so that's cool so
  • 00:13:22
    all right now
  • 00:13:25
    we've imported sqlite3
  • 00:13:27
    the next step is to create a connection
  • 00:13:30
    and
  • 00:13:30
    in order to do anything we need a
  • 00:13:32
    connection to our database so we need a
  • 00:13:36
    way for this file to sort of connect and
  • 00:13:38
    do stuff so to do this really easy we
  • 00:13:40
    just create a variable and you can call
  • 00:13:41
    it anything you want but i'm just going
  • 00:13:43
    to call this con short for connection
  • 00:13:45
    and then we want to set this equal to
  • 00:13:48
    sqlite3
  • 00:13:51
    dot connect
  • 00:13:53
    and then
  • 00:13:54
    we want to pass in
  • 00:13:56
    the name of the database that we want to
  • 00:13:58
    create now
  • 00:14:00
    this will connect to whatever we call
  • 00:14:03
    the database and if it doesn't exist it
  • 00:14:06
    will create it in this directory in this
  • 00:14:09
    sqlite directory that we just we just
  • 00:14:11
    created so we can call this anything we
  • 00:14:13
    want let's just call it um
  • 00:14:16
    customer.db we're going to pretend we
  • 00:14:18
    have a company we have customers so
  • 00:14:20
    we're going to create a database of
  • 00:14:21
    those customers so
  • 00:14:23
    that's
  • 00:14:24
    pretty much all we need to do
  • 00:14:26
    and
  • 00:14:28
    one thing i will note
  • 00:14:29
    sqlite3 allows you to create a database
  • 00:14:31
    like we've just done or you could also
  • 00:14:34
    just use a database in memory
  • 00:14:37
    and
  • 00:14:39
    it won't save it you can use it in your
  • 00:14:42
    program but then as soon as the program
  • 00:14:43
    ends the database disappears so you can
  • 00:14:45
    do stuff to it but it's not permanent
  • 00:14:48
    right so if you want to do that
  • 00:14:50
    i'm just going to copy this
  • 00:14:53
    and paste instead of naming the database
  • 00:14:56
    oops go back
  • 00:14:59
    instead of naming this database instead
  • 00:15:02
    we just type in colon
  • 00:15:04
    colon colon and then memory
  • 00:15:08
    and then another colon
  • 00:15:10
    right so this will create a connection
  • 00:15:12
    to a database in memory now we're not
  • 00:15:15
    going to do that in this course we want
  • 00:15:16
    to actually save our database so we can
  • 00:15:17
    use it later on if we want to but if
  • 00:15:20
    you've just got some little thing you
  • 00:15:21
    need to store some data do some stuff
  • 00:15:23
    too and then delete it afterwards this
  • 00:15:26
    memory thing will work so that's kind of
  • 00:15:28
    cool i'll just comment that out
  • 00:15:31
    okay so we've got our connection
  • 00:15:33
    and if we save this i'm going to hit
  • 00:15:35
    ctrl s on my keyboard
  • 00:15:39
    now we can run this it won't actually do
  • 00:15:41
    much well that's not actually true uh to
  • 00:15:43
    run this head over to your windows start
  • 00:15:45
    menu and type in git bash right just
  • 00:15:48
    search for git bash and that will open
  • 00:15:51
    up this
  • 00:15:53
    and if we go pwd we could see we're in
  • 00:15:55
    the c user's flat planet directory now
  • 00:15:58
    my computer is called flat planet it's
  • 00:16:00
    just the name of it i don't think the
  • 00:16:01
    world is flat i just find it hilarious
  • 00:16:03
    some people do so that's when i name my
  • 00:16:05
    computer whatever your computer name is
  • 00:16:07
    or whatever your username is you'll
  • 00:16:08
    likely see that there so what we want to
  • 00:16:10
    do first off is change directories we
  • 00:16:13
    want to move into that directory we just
  • 00:16:15
    created with our sublime text so to do
  • 00:16:18
    that we use the cd command stands for
  • 00:16:19
    change directory and then just type in c
  • 00:16:23
    and then
  • 00:16:25
    sqlite
  • 00:16:26
    i think that's the name of the directory
  • 00:16:28
    we just created with sublime if we type
  • 00:16:30
    in ls we can see our database.pi file
  • 00:16:34
    that we've just created and that is just
  • 00:16:37
    this file right here right database.pi
  • 00:16:40
    and there's not much going on in here
  • 00:16:42
    just yet
  • 00:16:43
    but we can still run this and to run it
  • 00:16:46
    as long as we're in this directory we
  • 00:16:48
    just type in python and then the name of
  • 00:16:50
    the file which is database dot pi
  • 00:16:53
    and if we do that it doesn't look like
  • 00:16:55
    anything's happened but now if we type
  • 00:16:57
    in ls
  • 00:16:58
    we see this customer.db file it's been
  • 00:17:01
    created and that is just this guy right
  • 00:17:05
    here so
  • 00:17:07
    the database wasn't wasn't there before
  • 00:17:09
    but when we ran this connection it
  • 00:17:11
    noticed that there wasn't a there wasn't
  • 00:17:13
    a database called customarydb and we're
  • 00:17:15
    trying to connect to customerdb so it
  • 00:17:17
    just creates it for us so that's a real
  • 00:17:19
    quick and easy way to create a database
  • 00:17:21
    and that's all for this video in the
  • 00:17:23
    next video we'll create a table and then
  • 00:17:25
    we'll start adding data to that table
  • 00:17:27
    and should be fun
  • 00:17:33
    all right so we've created our database
  • 00:17:34
    we have a connection now we want to
  • 00:17:36
    build a table in order to you know put
  • 00:17:39
    data in and if you're not really
  • 00:17:41
    familiar with databases a database
  • 00:17:44
    itself doesn't really do much it's the
  • 00:17:46
    table inside that has all the stuff in
  • 00:17:49
    it all the data goes into the table
  • 00:17:51
    whenever you query a database you're
  • 00:17:53
    querying a table whenever you're putting
  • 00:17:55
    information into a database you're
  • 00:17:56
    putting it into a table so it's the
  • 00:17:59
    table that's the important thing of a
  • 00:18:01
    database and think of a table as an
  • 00:18:03
    excel spreadsheet it's just a big thing
  • 00:18:06
    with rows and columns right that's
  • 00:18:09
    pretty much it so if you can visualize a
  • 00:18:11
    spreadsheet that's what a table is and
  • 00:18:13
    so to create a table
  • 00:18:16
    in uh sqlite 3 it's pretty simple first
  • 00:18:19
    we need to create a cursor and
  • 00:18:22
    a cursor
  • 00:18:23
    is
  • 00:18:25
    it's sort of like
  • 00:18:26
    i don't know it's it's it's what tells
  • 00:18:29
    the database what you want to do so
  • 00:18:31
    whenever you do anything you're going to
  • 00:18:32
    use your cursor to do it and you'll
  • 00:18:34
    understand more exactly what that means
  • 00:18:36
    as we start to use this it's really easy
  • 00:18:38
    uh we just have to build it once and
  • 00:18:40
    then we can use it so to to create a
  • 00:18:42
    cursor
  • 00:18:44
    we just create a variable and you can
  • 00:18:45
    call it anything you want you could call
  • 00:18:47
    it cursor if you wanted i'm just going
  • 00:18:49
    to call it c because we're going to be
  • 00:18:50
    typing it a lot so just c
  • 00:18:52
    so c equals
  • 00:18:55
    and we want to connect
  • 00:18:58
    to our connection this con
  • 00:19:01
    and then
  • 00:19:02
    we want to create a cursor instance
  • 00:19:04
    and you don't need to know what that
  • 00:19:05
    means it's just
  • 00:19:07
    it's the cursor right so we do that and
  • 00:19:10
    let's make a comment and i'm going to go
  • 00:19:12
    create
  • 00:19:14
    a cursor
  • 00:19:15
    right so now we can use this cursor to
  • 00:19:18
    do all kinds of things and the first
  • 00:19:21
    thing we want to do is create a table so
  • 00:19:22
    let's go create a table
  • 00:19:26
    now so to create a table it's really
  • 00:19:28
    easy we use our cursor c
  • 00:19:30
    and whenever we're we want to do things
  • 00:19:33
    in the database we're almost always
  • 00:19:35
    executing some command so we want to go
  • 00:19:37
    c dot execute
  • 00:19:40
    and then
  • 00:19:41
    pass in whatever commands we want in
  • 00:19:44
    this case we're going to create a table
  • 00:19:46
    and there's a couple of different ways
  • 00:19:48
    to execute commands using sql and the
  • 00:19:50
    first one i'm going to show you is
  • 00:19:52
    something called a
  • 00:19:54
    a doc string i think this was yeah doc
  • 00:19:56
    string and it's just
  • 00:19:59
    six
  • 00:20:00
    quotation marks
  • 00:20:01
    right
  • 00:20:02
    and inside of here
  • 00:20:04
    you can do multiple things on multiple
  • 00:20:07
    lines right that's why we want to use
  • 00:20:09
    this docs string and this is what the
  • 00:20:12
    python documentation recommends you do
  • 00:20:14
    now a lot of times when we execute a
  • 00:20:16
    command to our database we're just doing
  • 00:20:18
    one thing right
  • 00:20:20
    add
  • 00:20:21
    bob to the database right so that's one
  • 00:20:24
    line we won't use doc strings we'll just
  • 00:20:26
    use regular quotation marks but in this
  • 00:20:29
    instance when we're creating a whole
  • 00:20:31
    table there's going to be a lot of data
  • 00:20:32
    to push up we want to do it on multiple
  • 00:20:34
    lines just so it's easier to read and so
  • 00:20:37
    we'll use these docs strings so
  • 00:20:39
    what we want to do is create
  • 00:20:43
    a table
  • 00:20:44
    and what do we want to call our table
  • 00:20:46
    well our database is a customer database
  • 00:20:48
    so let's create a table of customers
  • 00:20:52
    plural so the customers table is in the
  • 00:20:55
    customer
  • 00:20:56
    singular database call it anything you
  • 00:20:58
    want though it doesn't really matter and
  • 00:21:00
    now
  • 00:21:01
    all we have to do now
  • 00:21:03
    is
  • 00:21:05
    define
  • 00:21:06
    the rows and columns that we want so
  • 00:21:10
    this is going to be on several lines i'm
  • 00:21:11
    just going to tab over here so what do
  • 00:21:13
    we want well we want to record customers
  • 00:21:17
    first names their last names and let's
  • 00:21:19
    say email so
  • 00:21:21
    we just sort of put all those things out
  • 00:21:24
    we type in first
  • 00:21:25
    and we want to call it first underscore
  • 00:21:28
    name
  • 00:21:28
    and then we have to decide the
  • 00:21:32
    data type
  • 00:21:33
    i'll just put data type for now we'll
  • 00:21:35
    talk about data types in a minute so
  • 00:21:37
    first name
  • 00:21:39
    last underscore name
  • 00:21:41
    data type
  • 00:21:45
    and email
  • 00:21:47
    or email address
  • 00:21:49
    let's just do email and then data type
  • 00:21:54
    okay
  • 00:21:56
    now we can
  • 00:22:00
    do this and let's
  • 00:22:02
    tab these over again okay so
  • 00:22:06
    i'll talk about data types in just a
  • 00:22:07
    second but let's look at this here we're
  • 00:22:10
    creating a table and one thing i should
  • 00:22:12
    note sqlite3 is case sensitive so the
  • 00:22:15
    things you type capitalized or lowercase
  • 00:22:18
    they matter so this needs to be capital
  • 00:22:20
    create table and then lowercase
  • 00:22:23
    customers
  • 00:22:24
    okay so let's look at this whole command
  • 00:22:26
    if i copy this
  • 00:22:28
    and print it again
  • 00:22:30
    just to give you an example without the
  • 00:22:32
    doc strings we could just use regular
  • 00:22:34
    quotation marks
  • 00:22:37
    but you see you got these big red angry
  • 00:22:40
    error messages here in our sublime text
  • 00:22:43
    that's because this all needs to go on
  • 00:22:46
    one line
  • 00:22:48
    if you use single quotation marks
  • 00:22:53
    and let's see
  • 00:22:57
    finally boom so okay
  • 00:23:00
    this will work right
  • 00:23:02
    this is equally valid this is valid and
  • 00:23:05
    this is valid but just look at this
  • 00:23:07
    right here like it's hard to read this
  • 00:23:09
    right
  • 00:23:10
    you know you gotta scroll
  • 00:23:13
    you know it's all smooshed together it's
  • 00:23:16
    just no good that's why i use these doc
  • 00:23:18
    string
  • 00:23:20
    triple quotation marks in this case so
  • 00:23:22
    just a a quick little aside so all right
  • 00:23:25
    we're almost there now we need to talk
  • 00:23:27
    about data types and
  • 00:23:29
    data types if you're familiar with
  • 00:23:31
    programming a data type is a type of
  • 00:23:34
    data so like think strings
  • 00:23:36
    numbers integers
  • 00:23:39
    booleans
  • 00:23:40
    um python has a bunch of other data
  • 00:23:42
    types like lists and
  • 00:23:45
    dictionaries
  • 00:23:47
    and things like that
  • 00:23:49
    if you're familiar with
  • 00:23:50
    data types it's the same thing here you
  • 00:23:52
    have to define the type of data that's
  • 00:23:55
    going to be in each of these fields so
  • 00:23:57
    in our case first name
  • 00:23:59
    last name and email those are all text
  • 00:24:01
    type things so we need a take that we
  • 00:24:03
    need to define a text data type now the
  • 00:24:05
    thing about sql lite that's actually
  • 00:24:07
    kind of cool i actually like is it only
  • 00:24:09
    has five data types that you can choose
  • 00:24:11
    from other databases mysql postgres any
  • 00:24:14
    other database you can think of
  • 00:24:16
    has
  • 00:24:17
    dozens of data types and it can get
  • 00:24:20
    confusing because you only really use a
  • 00:24:21
    couple right
  • 00:24:23
    if you think about it you're going to
  • 00:24:24
    use text you're going to use numbers
  • 00:24:26
    that's probably about it right so that's
  • 00:24:28
    kind of cool that
  • 00:24:30
    uh sqlite3 only has five and the five
  • 00:24:33
    are null
  • 00:24:37
    let's see
  • 00:24:38
    and
  • 00:24:39
    integer
  • 00:24:40
    i'll talk about these in just a sec here
  • 00:24:42
    uh real
  • 00:24:45
    text
  • 00:24:47
    and
  • 00:24:49
    blob
  • 00:24:51
    right so null just means
  • 00:24:54
    does it exist or does it not exist if it
  • 00:24:57
    doesn't exist it's null right if it does
  • 00:25:00
    exist it's not null right an integer is
  • 00:25:03
    a number a whole number 1 9 106 2
  • 00:25:07
    million right as opposed to real which
  • 00:25:11
    is a decimal
  • 00:25:12
    10.5 right 1995. that's real right so if
  • 00:25:16
    you're using something as a number just
  • 00:25:19
    use integer if you have decimals use
  • 00:25:22
    real text is just how it sounds text and
  • 00:25:25
    finally blob
  • 00:25:26
    is
  • 00:25:28
    it's it's it's extort it's stored
  • 00:25:30
    exactly as it is so think of like images
  • 00:25:33
    an image might be a blob um an mp3 file
  • 00:25:36
    a music file might be a blob right it's
  • 00:25:39
    just a blob right so pretty simple
  • 00:25:42
    in our case we're going to use
  • 00:25:45
    text for all of these
  • 00:25:47
    so we just type in text
  • 00:25:52
    text and
  • 00:25:55
    text
  • 00:25:57
    and that's it
  • 00:25:58
    right
  • 00:25:59
    uh let's go
  • 00:26:01
    data types
  • 00:26:03
    right
  • 00:26:04
    okay so we're almost there now
  • 00:26:06
    there's one more step actually two more
  • 00:26:08
    steps we need to do we've created this
  • 00:26:11
    cursor
  • 00:26:12
    right
  • 00:26:13
    and now we've
  • 00:26:15
    created a command for the cursor to do
  • 00:26:18
    but our program hasn't actually executed
  • 00:26:20
    that cursor command yet right in order
  • 00:26:23
    to do that we need to commit this thing
  • 00:26:25
    to the database and if you're familiar
  • 00:26:27
    with other databases committing
  • 00:26:29
    something to a database is a common
  • 00:26:30
    theme here to do it we just commit our
  • 00:26:33
    connection so con dot
  • 00:26:36
    commit
  • 00:26:38
    right and that's it this will now
  • 00:26:40
    execute this guy right here push
  • 00:26:43
    whatever we're trying to do into the
  • 00:26:44
    database and do whatever we're trying to
  • 00:26:47
    do
  • 00:26:47
    finally the last thing we really need to
  • 00:26:49
    do
  • 00:26:50
    and let's go
  • 00:26:51
    commit
  • 00:26:52
    our
  • 00:26:53
    command
  • 00:26:55
    the last thing we need to do is
  • 00:26:57
    close our
  • 00:26:59
    connection
  • 00:27:01
    so anytime
  • 00:27:03
    you create a connection like we did up
  • 00:27:04
    here right you want to close that
  • 00:27:07
    connection you don't have to when your
  • 00:27:09
    file stops running it will by default
  • 00:27:12
    close itself but it's just best practice
  • 00:27:14
    to do it explicitly and to do that we
  • 00:27:17
    just type in con.close
  • 00:27:20
    right
  • 00:27:21
    okay so if we save this
  • 00:27:24
    and now run it
  • 00:27:25
    pull up our terminal
  • 00:27:27
    clear the screen and i'm just going to
  • 00:27:29
    push up on my keyboard and that will
  • 00:27:32
    scroll to previous commands and we just
  • 00:27:34
    type in python database dot pi which is
  • 00:27:36
    the name of our file and we hit this
  • 00:27:38
    boom nothing has happened on the screen
  • 00:27:41
    but
  • 00:27:43
    uh
  • 00:27:44
    we didn't get any error messages so that
  • 00:27:46
    means that this thing executed correctly
  • 00:27:48
    we've now created a table called
  • 00:27:50
    customers inside of that customers table
  • 00:27:53
    there are three columns first name last
  • 00:27:55
    name and email and now we can start
  • 00:27:57
    sending data into the database and we'll
  • 00:28:00
    look at how to do that in the next video
  • 00:28:06
    okay so we've got our table created now
  • 00:28:08
    we want to put data into the table so
  • 00:28:11
    how do we do that well
  • 00:28:13
    pretty simple we just create another
  • 00:28:16
    cursor
  • 00:28:17
    command right so c dot execute
  • 00:28:20
    anytime we do anything we want to
  • 00:28:23
    execute
  • 00:28:24
    our c our cursor right and so now
  • 00:28:28
    we want to insert data into our table so
  • 00:28:32
    we use the insert into sql command and
  • 00:28:36
    this is all just sql if you're familiar
  • 00:28:38
    with sql sql these are
  • 00:28:41
    just sql commands right very basic so
  • 00:28:44
    they're always capitalized like that so
  • 00:28:46
    we want to insert into
  • 00:28:48
    customers the name of our table
  • 00:28:50
    and then values
  • 00:28:53
    and then
  • 00:28:54
    just
  • 00:28:57
    type in whatever values we want so
  • 00:28:59
    remember we have first name last name
  • 00:29:01
    and email so we need three things right
  • 00:29:04
    so
  • 00:29:06
    we want let's go
  • 00:29:09
    john
  • 00:29:11
    and we separate each of these by a comma
  • 00:29:13
    elder
  • 00:29:15
    comma
  • 00:29:17
    and then john at codamay.com
  • 00:29:20
    right so
  • 00:29:21
    you notice i'm using single quotes
  • 00:29:23
    that's because we started this string
  • 00:29:25
    off with a double quote so if we used if
  • 00:29:28
    we tried to use a
  • 00:29:29
    double quote here the whole thing would
  • 00:29:31
    be wonky because it would think that
  • 00:29:34
    we're trying to close this double quote
  • 00:29:36
    instead we just use single quotes and
  • 00:29:38
    everything works fine so let's see that
  • 00:29:40
    worked
  • 00:29:42
    that looks good
  • 00:29:43
    now again we need to commit
  • 00:29:47
    this command so we'll just leave that
  • 00:29:48
    there and we need to close our
  • 00:29:50
    connection so let's go ahead and save
  • 00:29:51
    this
  • 00:29:53
    come back here and
  • 00:29:55
    run this again and again we get no
  • 00:29:57
    message now we could create a little
  • 00:29:59
    message if we wanted to
  • 00:30:01
    you know we could just print to the
  • 00:30:02
    screen
  • 00:30:05
    command executed
  • 00:30:09
    successfully
  • 00:30:12
    i don't know you know if we wanted to do
  • 00:30:13
    that we could we definitely don't have
  • 00:30:15
    to do that so we've now inserted one
  • 00:30:18
    record one
  • 00:30:20
    row into our database we could do more
  • 00:30:23
    than that we could go tim
  • 00:30:25
    tim
  • 00:30:27
    smith
  • 00:30:29
    let's go tim at codamy.com so if we save
  • 00:30:33
    this come back here run it again
  • 00:30:36
    command execute it successfully that
  • 00:30:38
    makes you feel a little better that
  • 00:30:40
    something is on the screen right
  • 00:30:42
    whatever uh let's do one more let's go
  • 00:30:45
    uh mary
  • 00:30:48
    that's a good last name
  • 00:30:50
    mary
  • 00:30:52
    brown
  • 00:30:53
    i don't know
  • 00:30:54
    and she's at mary kotami.com
  • 00:30:58
    save this
  • 00:31:00
    run it again
  • 00:31:03
    okay so now we've got three records in
  • 00:31:05
    our database
  • 00:31:07
    pretty simple
  • 00:31:09
    to sort of add one thing at a time to
  • 00:31:11
    our database
  • 00:31:12
    and uh yeah that's all for this video
  • 00:31:20
    okay in the last video we inserted one
  • 00:31:22
    value at a time and sometimes you want
  • 00:31:24
    to do that but sometimes you want to
  • 00:31:27
    sort of insert lots of things all at
  • 00:31:29
    once how do you do that well it's a
  • 00:31:31
    little bit more complicated but not too
  • 00:31:32
    much more complicated so let's create
  • 00:31:36
    um i don't know many
  • 00:31:40
    customers
  • 00:31:41
    a variable and we want this to be a
  • 00:31:43
    python list right so inside of here
  • 00:31:48
    each
  • 00:31:49
    item
  • 00:31:51
    as you would with any python item
  • 00:31:54
    you can
  • 00:31:55
    go like this
  • 00:31:57
    right so each of these things become an
  • 00:32:00
    item
  • 00:32:01
    right
  • 00:32:02
    so it's like a tuple inside of a list
  • 00:32:04
    right and here you would just go
  • 00:32:08
    let's go wes
  • 00:32:10
    at
  • 00:32:12
    brown
  • 00:32:15
    and let's go wes at
  • 00:32:19
    brown.com right
  • 00:32:23
    and we can put these on multiple lines
  • 00:32:25
    if we want
  • 00:32:33
    to sort of just make it easier to read
  • 00:32:35
    we can go
  • 00:32:36
    um
  • 00:32:38
    i don't know
  • 00:32:40
    staff
  • 00:32:43
    uh
  • 00:32:45
    [Music]
  • 00:32:47
    steph at
  • 00:32:53
    and finally we can go
  • 00:32:56
    dan
  • 00:32:59
    pass
  • 00:33:02
    and dan's email address is dan at
  • 00:33:05
    pass.com
  • 00:33:07
    all right
  • 00:33:09
    i'm gonna go ahead and put a
  • 00:33:12
    comma there at the end of that okay so
  • 00:33:14
    now
  • 00:33:15
    we need to do a little bit
  • 00:33:18
    different down here in our execute
  • 00:33:19
    instead of executing we're going to
  • 00:33:22
    execute many
  • 00:33:23
    right
  • 00:33:24
    slightly different command there
  • 00:33:26
    and here instead of
  • 00:33:28
    typing in
  • 00:33:30
    all the things we want to
  • 00:33:33
    create a tuple and we want to put
  • 00:33:35
    placeholders so the placeholder in sql
  • 00:33:37
    i3 is the
  • 00:33:38
    question mark so we've got a question
  • 00:33:40
    mark question mark question mark so this
  • 00:33:43
    is sort of first name last name email
  • 00:33:45
    right
  • 00:33:47
    and then
  • 00:33:48
    we put a comma and we just pass in the
  • 00:33:51
    name of our list here right many
  • 00:33:53
    customer
  • 00:33:55
    actually this changes many customers
  • 00:33:59
    makes more sense right
  • 00:34:00
    okay so
  • 00:34:03
    what's going to do here is it's going to
  • 00:34:04
    execute many
  • 00:34:06
    each of these is one two three
  • 00:34:09
    and
  • 00:34:10
    here's the placeholder for each one
  • 00:34:13
    pretty simple if we save this
  • 00:34:16
    and run it again
  • 00:34:19
    command executed successfully so
  • 00:34:21
    hopefully that has worked we don't know
  • 00:34:23
    yet if any of these have worked because
  • 00:34:25
    we haven't actually looked inside of our
  • 00:34:27
    database to see what's in there and
  • 00:34:29
    we'll do that in the next video
  • 00:34:35
    okay so we've put a bunch of stuff into
  • 00:34:37
    the database now how do we get anything
  • 00:34:38
    out and sort of display it see what's in
  • 00:34:40
    there well to do that we need to query
  • 00:34:42
    the database
  • 00:34:44
    and to do that we use our cursor
  • 00:34:45
    c.execute
  • 00:34:47
    there we go
  • 00:34:48
    and then inside of here we want to
  • 00:34:51
    select
  • 00:34:54
    and let's just pull out everything at
  • 00:34:56
    this point so star stands for everything
  • 00:34:58
    from
  • 00:34:59
    and we want to pull it out from the
  • 00:35:01
    customers
  • 00:35:05
    table
  • 00:35:06
    all right so
  • 00:35:08
    that
  • 00:35:09
    sort of creates the query
  • 00:35:11
    and
  • 00:35:12
    to actually
  • 00:35:14
    get the thing we need to then go c
  • 00:35:18
    dot
  • 00:35:19
    fetch
  • 00:35:20
    and
  • 00:35:21
    you can do three different things i'll
  • 00:35:23
    just type them out we can fetch one
  • 00:35:26
    we can c dot fetch
  • 00:35:29
    many and then pass in how many however
  • 00:35:32
    many we want or we can c dot
  • 00:35:35
    fetch
  • 00:35:36
    all
  • 00:35:37
    and this will return as you would expect
  • 00:35:40
    everything this will just fetch the last
  • 00:35:42
    the last item in the table
  • 00:35:45
    yeah uh so what we want to do is fetch
  • 00:35:48
    all so if we run this nothing actually
  • 00:35:51
    happens we have to actually print to the
  • 00:35:54
    screen explicitly so we would wrap this
  • 00:35:56
    whole thing in a print function
  • 00:35:59
    and then we can go ahead and
  • 00:36:03
    comment out that so let's save this and
  • 00:36:05
    run it and see what we got here
  • 00:36:09
    and boom we get all of this stuff and
  • 00:36:11
    this is returned as
  • 00:36:14
    a python list right so here we have john
  • 00:36:17
    elder that was the first one tim smith
  • 00:36:20
    mary brown
  • 00:36:22
    and then
  • 00:36:24
    that time in the last video when we did
  • 00:36:26
    them all at once we had wes steph and
  • 00:36:29
    dan
  • 00:36:30
    and you can see all of them
  • 00:36:33
    are uh sort of
  • 00:36:36
    just sort of spewing up on the screen
  • 00:36:37
    right so this is just all the data
  • 00:36:40
    given at once
  • 00:36:41
    obviously you can do all kinds of stuff
  • 00:36:43
    to pull out specific stuff to make this
  • 00:36:46
    look more pre readable pretty you know
  • 00:36:49
    useful and i think we'll look at that in
  • 00:36:51
    the next video
  • 00:36:57
    okay so we fetched all right right off
  • 00:36:59
    the bat
  • 00:37:01
    it spewed back all this stuff we could
  • 00:37:03
    also
  • 00:37:05
    let's go ahead and comment this out
  • 00:37:08
    and we could just fetch one and if we
  • 00:37:10
    want to print this to the screen we wrap
  • 00:37:12
    it all on print function we save this
  • 00:37:14
    and run it just real quick just to show
  • 00:37:16
    you what this other stuff does
  • 00:37:18
    boom john elder john codamy.com
  • 00:37:21
    you can see it looks like a tuple tuple
  • 00:37:23
    however you want to say that and you can
  • 00:37:25
    access it just as a tuple so you notice
  • 00:37:28
    this is the very first thing so it's not
  • 00:37:30
    the last thing that fetch one brings you
  • 00:37:32
    it's the first thing right
  • 00:37:34
    likewise you could do
  • 00:37:38
    let's see fetch many
  • 00:37:41
    let's comment this out and we want to
  • 00:37:42
    wrap this
  • 00:37:46
    save this and run it
  • 00:37:49
    we get three
  • 00:37:51
    one two and three it's just the first
  • 00:37:53
    second and third one right so kind of
  • 00:37:56
    interesting
  • 00:37:58
    but
  • 00:37:58
    we don't wanna do that we wanna work
  • 00:38:00
    with fetch all
  • 00:38:02
    and now we to do stuff
  • 00:38:04
    right
  • 00:38:05
    so
  • 00:38:06
    well actually first before we do that
  • 00:38:09
    let's go back to the fetch one
  • 00:38:12
    now this is
  • 00:38:15
    a tuple
  • 00:38:17
    so we can access the stuff inside of it
  • 00:38:19
    by referencing
  • 00:38:21
    the the item number so first off before
  • 00:38:23
    we do that let's just save this and run
  • 00:38:24
    it again just to show you
  • 00:38:26
    real quick i'm gonna clear the screen
  • 00:38:30
    okay so fetch one brings
  • 00:38:33
    one record john elder and john
  • 00:38:34
    codomy.com so this is the zero with item
  • 00:38:37
    in the tuple this is the first item
  • 00:38:40
    and this is the second item this is
  • 00:38:42
    python tuple stuff you're familiar with
  • 00:38:43
    tuples or lists when you number items in
  • 00:38:46
    a list or a tuple the first one is zero
  • 00:38:49
    the second one is one and the third one
  • 00:38:52
    is two right so we can access specific
  • 00:38:54
    ones of those by referencing those
  • 00:38:56
    numbers and to do that
  • 00:38:58
    we just like i said slap on the
  • 00:39:01
    brackets here and let's just call zero
  • 00:39:03
    the zero with item that should be john
  • 00:39:05
    so if we save this and run it
  • 00:39:07
    boom we get john so that is one way to
  • 00:39:09
    do it right and you can do the same
  • 00:39:11
    thing with fetch
  • 00:39:13
    fetch all
  • 00:39:14
    and fetch many for that matter but
  • 00:39:17
    that's not really what we want to look
  • 00:39:19
    at right now
  • 00:39:20
    what we want to look at is fetch all
  • 00:39:23
    and
  • 00:39:26
    this is
  • 00:39:27
    a list right so we could
  • 00:39:32
    set a new variable we could create a
  • 00:39:34
    variable called um
  • 00:39:36
    items
  • 00:39:37
    and set it equal to this
  • 00:39:40
    then we could either just print
  • 00:39:42
    items
  • 00:39:44
    in fact if we do this
  • 00:39:47
    it'll look the same
  • 00:39:48
    save this and run it as it did earlier
  • 00:39:51
    just spews out all of it right
  • 00:39:54
    or you could
  • 00:39:57
    create a loop of some sort to loop
  • 00:39:59
    through each thing
  • 00:40:01
    let's go ahead and give that a try
  • 00:40:04
    so let's go
  • 00:40:07
    for item in items
  • 00:40:11
    print
  • 00:40:13
    item
  • 00:40:15
    okay so if we save this
  • 00:40:18
    and run it just a basic for loop boom we
  • 00:40:22
    have each of these printed out
  • 00:40:25
    one line at a time now we're starting to
  • 00:40:27
    get somewhere it's starting to be a
  • 00:40:29
    little bit more readable right so each
  • 00:40:31
    of these are now a tuple and we can do
  • 00:40:34
    tupley things to them so instead of
  • 00:40:38
    printing out the whole item
  • 00:40:40
    we could print out
  • 00:40:42
    use our brackets and say print out the
  • 00:40:44
    zeroth item inside each tuple all right
  • 00:40:47
    so if we save this
  • 00:40:49
    and run it
  • 00:40:52
    we get john 10 mary west steph and dan
  • 00:40:55
    very cool
  • 00:40:57
    there are three
  • 00:41:00
    columns so we could if we really wanted
  • 00:41:03
    to play around with this we could go
  • 00:41:05
    print
  • 00:41:06
    this
  • 00:41:07
    plus we can then concatenate
  • 00:41:10
    and let's make a space and concatenate
  • 00:41:12
    again and let's go item
  • 00:41:16
    one
  • 00:41:17
    concatenate
  • 00:41:18
    make another space
  • 00:41:20
    and then concatenate again and go item
  • 00:41:24
    2
  • 00:41:25
    right so if we save this and run it
  • 00:41:29
    and let's clear the screen
  • 00:41:32
    we get john elder john codyme.com tim
  • 00:41:35
    smith right so it's starting to format
  • 00:41:37
    up a little bit
  • 00:41:39
    and we could play around with this you
  • 00:41:40
    know we could uh do a little formatting
  • 00:41:45
    like that
  • 00:41:47
    if we save this and run it
  • 00:41:51
    and that's looking a little bit better
  • 00:41:53
    we could um oh i don't know we could use
  • 00:41:56
    tabs instead
  • 00:41:59
    that
  • 00:42:00
    is backslash t
  • 00:42:04
    is that right there's a forward slash
  • 00:42:07
    backslash so this will tab everything
  • 00:42:09
    over
  • 00:42:11
    so if we
  • 00:42:12
    run this
  • 00:42:14
    and now things are starting to look a
  • 00:42:16
    little bit better this one didn't quite
  • 00:42:17
    make it over because this
  • 00:42:20
    it did tab but it
  • 00:42:22
    this name is so short the tab didn't go
  • 00:42:25
    as far
  • 00:42:26
    that's kind of funny
  • 00:42:30
    we could do
  • 00:42:32
    two tabs i suppose
  • 00:42:35
    right
  • 00:42:36
    and here we could go
  • 00:42:39
    outside of the loop
  • 00:42:41
    we could go
  • 00:42:42
    name
  • 00:42:45
    well we need quotation marks name
  • 00:42:50
    yeah
  • 00:42:52
    plus
  • 00:42:54
    two tabs
  • 00:42:57
    and email
  • 00:43:00
    so i'll look right
  • 00:43:02
    and here we could do print and
  • 00:43:05
    one two three four
  • 00:43:08
    five
  • 00:43:10
    plus
  • 00:43:13
    a couple of tabs
  • 00:43:15
    and then what one two three four five
  • 00:43:17
    six
  • 00:43:20
    seven
  • 00:43:21
    eight
  • 00:43:22
    i don't know
  • 00:43:23
    a couple more boom
  • 00:43:25
    i don't know save this i'm just playing
  • 00:43:26
    around at this point because it's kind
  • 00:43:28
    of fun
  • 00:43:30
    all right formatting has gone a little
  • 00:43:31
    bit wonky here this is too short
  • 00:43:35
    but you get the idea right you can sort
  • 00:43:37
    of
  • 00:43:39
    play around with all this stuff
  • 00:43:41
    let's get rid of
  • 00:43:45
    one of these tabs see how that looks
  • 00:43:50
    clear the screen real quick
  • 00:43:53
    all right so looking a little better
  • 00:43:55
    the point is you can use all the sort of
  • 00:43:57
    pythonic things you want loops
  • 00:44:00
    things like that to format the
  • 00:44:02
    stuff that you fetched all
  • 00:44:04
    in any way you want and you use it just
  • 00:44:06
    by calling any list item in the returned
  • 00:44:10
    data so
  • 00:44:12
    a little bit
  • 00:44:14
    much to to
  • 00:44:16
    swallow in this one but uh really pretty
  • 00:44:18
    simple when you get right down to it all
  • 00:44:20
    the stuff at the end which is playing
  • 00:44:21
    around here so
  • 00:44:22
    that's how you
  • 00:44:24
    sort of format results
  • 00:44:26
    using python and sql i3
  • 00:44:32
    okay in this video i want to talk about
  • 00:44:34
    primary keys and a primary key is a
  • 00:44:37
    database thing if you're familiar with
  • 00:44:39
    other databases you're probably already
  • 00:44:40
    aware of what a primary key is and
  • 00:44:43
    basically it's a unique id number that
  • 00:44:45
    each record in your database gets so
  • 00:44:49
    for instance we've got
  • 00:44:52
    we've select everything from our data
  • 00:44:54
    database table customers and we've put
  • 00:44:57
    it into this items variable now we could
  • 00:45:00
    print out items right if we save this
  • 00:45:04
    and run it
  • 00:45:07
    oops
  • 00:45:08
    there we go we see each of these items
  • 00:45:11
    john elder
  • 00:45:12
    tim blah blah blah we can like we did in
  • 00:45:15
    the last video a couple of videos
  • 00:45:17
    instead we can go
  • 00:45:19
    um
  • 00:45:20
    for item in
  • 00:45:22
    items
  • 00:45:25
    print
  • 00:45:26
    item
  • 00:45:27
    if we save this it looks a little bit
  • 00:45:29
    nicer
  • 00:45:31
    right so each of these is a record this
  • 00:45:34
    is a record this is a record this is a
  • 00:45:36
    record right and you know each record
  • 00:45:39
    has a column first name a column last
  • 00:45:41
    name and a column email well
  • 00:45:43
    actually sqlite 3 creates another column
  • 00:45:47
    sort of in the background with a
  • 00:45:49
    specific primary key a unique id that
  • 00:45:52
    is unique for each of these records and
  • 00:45:55
    to see what that is it's called a row id
  • 00:45:58
    and we can actually select
  • 00:46:01
    right up here in our select statement we
  • 00:46:03
    can just go row id
  • 00:46:05
    and everything from customers now if we
  • 00:46:08
    save this
  • 00:46:09
    and run it
  • 00:46:11
    it's going to print out that id as well
  • 00:46:13
    so you can see one two three four five
  • 00:46:15
    six
  • 00:46:17
    john
  • 00:46:18
    two tim three mary so you know
  • 00:46:21
    we could look up mary brown or we could
  • 00:46:24
    look up
  • 00:46:25
    number three right and we'll look at how
  • 00:46:28
    to
  • 00:46:28
    call specific
  • 00:46:30
    things from our database in the next
  • 00:46:32
    video but in this video i just wanted to
  • 00:46:34
    really quickly touch base on that
  • 00:46:35
    primary key because it's an important
  • 00:46:36
    database concept and in most databases
  • 00:46:39
    you have to create the primary key
  • 00:46:41
    yourself and then create some sort of
  • 00:46:43
    auto incrementing mechanism so that
  • 00:46:46
    anytime you add a new record to the
  • 00:46:47
    database it auto increments a new id so
  • 00:46:51
    for instance we have six right six is
  • 00:46:53
    our last id if we added a new person in
  • 00:46:56
    we would need to create something that
  • 00:46:57
    said okay
  • 00:46:58
    the last one was six so make the next
  • 00:47:00
    one seven right we don't have to do any
  • 00:47:02
    of that with sql light it does it for us
  • 00:47:04
    automatically unless you specifically
  • 00:47:06
    tell it not to and i'm not even going to
  • 00:47:09
    get into showing you how to do that
  • 00:47:10
    because
  • 00:47:11
    you're never not going to want to do
  • 00:47:12
    that you're always going to want this id
  • 00:47:14
    it it's a good idea to have a unique
  • 00:47:18
    number for each record that you can
  • 00:47:19
    reference and we'll see why a little bit
  • 00:47:21
    later on in the course so i just really
  • 00:47:23
    quickly wanted to show you that row id
  • 00:47:26
    uh it's just sort of a unique sql light
  • 00:47:28
    3 3 thing it does it for you it's very
  • 00:47:31
    very cool
  • 00:47:32
    and uh yeah so that's all for this video
  • 00:47:33
    in the next video we'll look at pulling
  • 00:47:35
    out specific things from our database
  • 00:47:38
    like searching for specific things and
  • 00:47:40
    that'll be in the next video
  • 00:47:46
    okay up until now we've just been
  • 00:47:47
    pulling everything from the database
  • 00:47:50
    from the table now we want to look at
  • 00:47:52
    how to pull out specific things how to
  • 00:47:54
    search for things right so you maybe
  • 00:47:56
    don't want everybody you might want
  • 00:47:58
    uh just everybody with the name last
  • 00:48:01
    name elder how would you do that well
  • 00:48:03
    come up to our query here and so we want
  • 00:48:06
    to let's get rid of the row id we don't
  • 00:48:08
    really need that we want to select
  • 00:48:09
    everything from customers and now we
  • 00:48:12
    just use the where clause where this is
  • 00:48:14
    just basic sql where
  • 00:48:17
    we could go last underscore name
  • 00:48:20
    equals
  • 00:48:22
    and use single quotation marks
  • 00:48:25
    uh elder right
  • 00:48:27
    so if we save this and run it
  • 00:48:32
    let's clear the screen here
  • 00:48:35
    we get boom john elder
  • 00:48:38
    that's cool
  • 00:48:39
    now
  • 00:48:40
    we can also use all of our other sort of
  • 00:48:43
    comparison operators right if we had
  • 00:48:45
    numbers we could use uh less than equal
  • 00:48:49
    to
  • 00:48:51
    greater than greater than or equal to
  • 00:48:54
    you know where let's say if we had an
  • 00:48:56
    age column we don't but if we had an age
  • 00:48:58
    column where age is greater than let's
  • 00:49:00
    say we wanted everybody over the age of
  • 00:49:02
    21 right
  • 00:49:03
    or equal to 21. we would do that right
  • 00:49:06
    so that's kind of cool one you're going
  • 00:49:08
    to use a lot is like so we could go
  • 00:49:12
    where
  • 00:49:13
    last underscore name is like
  • 00:49:16
    and then use quotation marks to sort of
  • 00:49:19
    search and we've got a bunch of browns
  • 00:49:21
    so we could go where the name is sort of
  • 00:49:22
    like br and then you put this uh
  • 00:49:26
    what is that the percentage sign
  • 00:49:29
    yeah to it's sort of like a wild card
  • 00:49:31
    where it starts with br and ends with
  • 00:49:34
    whatever right so if we save this and
  • 00:49:37
    run it
  • 00:49:40
    we get mary brown and wes brown right so
  • 00:49:44
    uh very very cool
  • 00:49:46
    uh we've got a bunch of kodamies right
  • 00:49:49
    we could play around with that where we
  • 00:49:52
    can go where email
  • 00:49:54
    is like and we can start out with this
  • 00:49:57
    percentage sign and then we can go
  • 00:49:59
    kodamy.com
  • 00:50:02
    save this
  • 00:50:04
    i'm not sure why that turned purple like
  • 00:50:07
    that
  • 00:50:08
    but we get
  • 00:50:10
    three returned results
  • 00:50:12
    uh let's look at this more let's go
  • 00:50:14
    let's get rid of this yeah it's just
  • 00:50:16
    gonna do that it looks like so okay so
  • 00:50:19
    there were three now we can test this
  • 00:50:21
    just to make sure
  • 00:50:22
    that was correct right
  • 00:50:25
    and let's save this and just run it
  • 00:50:26
    again
  • 00:50:29
    so we have one two three
  • 00:50:32
    and that's all
  • 00:50:34
    so that was correct so a pretty simple
  • 00:50:36
    way
  • 00:50:38
    to
  • 00:50:39
    search for different things using your
  • 00:50:41
    query using the where clause and like i
  • 00:50:44
    said you're going to use your logical
  • 00:50:45
    operators
  • 00:50:46
    those are basically
  • 00:50:49
    you know
  • 00:50:51
    like i said equal to
  • 00:50:53
    less than greater than less than or
  • 00:50:55
    equal to greater than or equal to
  • 00:50:58
    uh like
  • 00:50:59
    and that's pretty cool so in the next
  • 00:51:01
    video we'll look at updating records
  • 00:51:03
    making changes once we've already put
  • 00:51:05
    something in the database and that'll be
  • 00:51:06
    in the next video
  • 00:51:12
    okay in this video we want to update
  • 00:51:14
    records so
  • 00:51:16
    let's head over here let's just put this
  • 00:51:18
    stuff down and let's go update records
  • 00:51:24
    and it's pretty simple pretty
  • 00:51:25
    straightforward
  • 00:51:26
    and we just go see
  • 00:51:29
    there we go c dot execute
  • 00:51:33
    right now this is going to actually be
  • 00:51:35
    kind of long so we're going to use those
  • 00:51:36
    doc strings again
  • 00:51:38
    one two three four five six
  • 00:51:40
    right
  • 00:51:42
    and what we want to do is update
  • 00:51:46
    and we want to update our customers
  • 00:51:48
    table right now we want to set what do
  • 00:51:52
    we want to set well let's say we want to
  • 00:51:53
    change the first name of john elder to
  • 00:51:57
    bob elder right so we want to set
  • 00:52:00
    first name
  • 00:52:03
    equal to
  • 00:52:05
    bob
  • 00:52:07
    and then
  • 00:52:09
    where
  • 00:52:12
    and let's go last name
  • 00:52:15
    equals
  • 00:52:16
    elder right
  • 00:52:23
    and
  • 00:52:25
    actually we want to wrap these in
  • 00:52:28
    single quotes
  • 00:52:31
    there we go
  • 00:52:34
    boom
  • 00:52:37
    ah come back
  • 00:52:39
    i cannot type there we go all right so
  • 00:52:41
    we could put this all in one line but
  • 00:52:43
    easier to break it apart like that so
  • 00:52:45
    this will work but it's probably not the
  • 00:52:47
    way you want to do it we'll talk about
  • 00:52:49
    that in just a second so let's go ahead
  • 00:52:51
    and
  • 00:52:52
    we want to
  • 00:52:56
    commit this
  • 00:52:58
    so we'll go see commit
  • 00:53:00
    or con.commit right and then we want to
  • 00:53:04
    select everything from our database and
  • 00:53:06
    print it all onto the screen just to
  • 00:53:07
    make sure that the change got made right
  • 00:53:09
    so let's save this
  • 00:53:11
    head back over here and run it again and
  • 00:53:14
    boom we get bob elder
  • 00:53:16
    tim smith everything else is the same
  • 00:53:19
    and it looks like it's worked
  • 00:53:20
    now
  • 00:53:23
    this isn't a good way to do it
  • 00:53:25
    i mean it works and this is the correct
  • 00:53:28
    structure and everything but you're not
  • 00:53:30
    going to want to make a change to a
  • 00:53:31
    record
  • 00:53:32
    based on a last name because we could
  • 00:53:34
    have a bunch of elders in there and if
  • 00:53:36
    we did that this would change them all
  • 00:53:39
    and we don't want to necessarily do that
  • 00:53:40
    right so instead
  • 00:53:45
    we want to come down here
  • 00:53:47
    and let's call our row id
  • 00:53:50
    again
  • 00:53:51
    let's save this and run it
  • 00:53:56
    and now we see
  • 00:53:58
    our row ids what you really want to do
  • 00:54:00
    is use those row ids for this exact
  • 00:54:02
    thing right we want we don't want to
  • 00:54:04
    update all the records that have a last
  • 00:54:07
    name elder we just want to update record
  • 00:54:10
    number one so instead we want to do it
  • 00:54:13
    that way so let's let's go ahead and try
  • 00:54:14
    that again and up here we want to set
  • 00:54:17
    first name equal let's change it back to
  • 00:54:19
    john
  • 00:54:20
    where
  • 00:54:21
    and then we go
  • 00:54:23
    row id
  • 00:54:25
    equals
  • 00:54:26
    one all right so if we save this
  • 00:54:29
    and run this again
  • 00:54:32
    boom
  • 00:54:33
    now
  • 00:54:35
    it's john elder
  • 00:54:36
    right
  • 00:54:37
    so like down here brown
  • 00:54:40
    we want to change mary brown to marty
  • 00:54:42
    brown right so she's number three if
  • 00:54:45
    instead we did brown that would throw
  • 00:54:48
    off some problems right so let's try
  • 00:54:50
    that and just see what happens so
  • 00:54:52
    we want to go marty
  • 00:54:54
    where
  • 00:54:59
    last underscore name
  • 00:55:01
    equals brown
  • 00:55:03
    right
  • 00:55:04
    so if we save this and run it
  • 00:55:10
    boom this one is marty brown and this
  • 00:55:12
    one is marty brown see
  • 00:55:15
    that's no good we can't have that right
  • 00:55:18
    so
  • 00:55:18
    instead
  • 00:55:20
    let's change it back to mary
  • 00:55:23
    and we can do that like this
  • 00:55:27
    save this and run it
  • 00:55:31
    that gets us part of the way now they're
  • 00:55:32
    both mary brown but we want number four
  • 00:55:35
    to beat wes brown
  • 00:55:36
    right instead of mary so
  • 00:55:39
    we would come up here
  • 00:55:41
    where
  • 00:55:43
    row i d
  • 00:55:45
    equals four
  • 00:55:48
    is that right
  • 00:55:52
    which was wes west was plus yep four
  • 00:55:55
    all right so then we want to change it
  • 00:55:57
    to
  • 00:55:58
    wes
  • 00:55:58
    so if we save this
  • 00:56:01
    let's clear the screen
  • 00:56:03
    and run this one more time
  • 00:56:06
    we get number three mary brown number
  • 00:56:08
    four wes brown now everything is right
  • 00:56:12
    in the world so important that you use
  • 00:56:14
    id the row id the primary key in order
  • 00:56:17
    to
  • 00:56:18
    update things also to delete things
  • 00:56:20
    we'll look at that next
  • 00:56:22
    uh you know just very important
  • 00:56:24
    otherwise you can get in all kinds of
  • 00:56:25
    problems like we just saw
  • 00:56:27
    and that's no good so that's all for
  • 00:56:28
    this video in the next video we'll look
  • 00:56:30
    at how to delete a record
  • 00:56:36
    okay in the last video we looked at
  • 00:56:37
    updating records in this video we want
  • 00:56:39
    to look at deleting and deleting is very
  • 00:56:41
    similar to updating it's pretty simple
  • 00:56:43
    we just
  • 00:56:45
    come over here and go c dot execute
  • 00:56:49
    and then inside of here we want to
  • 00:56:51
    delete
  • 00:56:53
    from
  • 00:56:54
    our customers
  • 00:56:56
    table
  • 00:56:57
    and just use our where clause where
  • 00:57:00
    and then what
  • 00:57:01
    well
  • 00:57:05
    our data is gone
  • 00:57:09
    but
  • 00:57:10
    the last row
  • 00:57:12
    was that dan
  • 00:57:15
    guy he was number six right and uh let's
  • 00:57:18
    get rid of him we had trouble with the
  • 00:57:20
    formatting because his name was too
  • 00:57:22
    small way back at the beginning of the
  • 00:57:23
    course so let's just get rid of him so
  • 00:57:25
    here we go row
  • 00:57:26
    id equals 6. now you'll notice
  • 00:57:29
    i'm not wrapping this in quotation marks
  • 00:57:32
    because that would make this a string
  • 00:57:35
    and the row id is a number it's an
  • 00:57:36
    integer so you can't use a string when
  • 00:57:38
    it's a number so just sort of keep that
  • 00:57:41
    in mind and i think that's pretty much
  • 00:57:43
    it so if we save this
  • 00:57:46
    head over here and run it again
  • 00:57:48
    boom number six dan pass is gone
  • 00:57:52
    so that's delete
  • 00:57:54
    once you delete a thing it's gone uh dan
  • 00:57:57
    has been dropped from the table and when
  • 00:57:58
    you delete things we call it dropping
  • 00:58:00
    from a table so he's been dropped he's
  • 00:58:02
    deleted he's gone
  • 00:58:04
    and uh that's that he can't come back so
  • 00:58:06
    keep that in mind when you delete
  • 00:58:07
    something it's sort of permanent so uh
  • 00:58:10
    yeah just keep that in mind oh so that's
  • 00:58:12
    all there is to delete pretty simple and
  • 00:58:14
    in the next video we'll look at ordering
  • 00:58:20
    okay in this video i want to look at
  • 00:58:22
    ordering and ordering allows us to
  • 00:58:24
    return the results however we want right
  • 00:58:27
    now we're just returning them by default
  • 00:58:29
    which is just in order one two three
  • 00:58:32
    four five right so we
  • 00:58:34
    could order by just about anything
  • 00:58:38
    and um
  • 00:58:40
    well we'll just go through and do some
  • 00:58:42
    and you'll see so let's see let's change
  • 00:58:45
    this to
  • 00:58:47
    order by
  • 00:58:49
    in fact we can get rid of all this
  • 00:58:53
    instead of query
  • 00:58:56
    the database let's go
  • 00:58:58
    order by
  • 00:59:00
    all right so same sort of thing right we
  • 00:59:03
    want to select row id and everything
  • 00:59:06
    from customers
  • 00:59:07
    and we just go order
  • 00:59:11
    by
  • 00:59:12
    and then let's go
  • 00:59:15
    row id
  • 00:59:17
    right so if we save this and run it
  • 00:59:18
    we're not really going to see anything
  • 00:59:20
    different here
  • 00:59:22
    right one two three four five
  • 00:59:24
    because default that's how things go
  • 00:59:27
    ascending from small to high
  • 00:59:30
    that's ascending
  • 00:59:31
    or
  • 00:59:32
    uh a
  • 00:59:33
    asc ascending short for a sen
  • 00:59:37
    ding right you can also do descending d
  • 00:59:40
    e s c
  • 00:59:42
    short for descending right
  • 00:59:44
    and descending is high to low so we can
  • 00:59:47
    do that
  • 00:59:48
    just by changing this
  • 00:59:50
    to desc
  • 00:59:52
    all right so if we save this and run it
  • 00:59:56
    boom now
  • 00:59:58
    see the first one listed is five four
  • 01:00:00
    three two one right
  • 01:00:02
    pretty cool
  • 01:00:04
    you know you could
  • 01:00:06
    type in asc here but the default is
  • 01:00:09
    ascending so you don't have to be
  • 01:00:11
    explicit right
  • 01:00:13
    we could order by last name
  • 01:00:18
    now that's going to be alphabetical from
  • 01:00:21
    low to high or from a to z right so if
  • 01:00:25
    we save this and run it let's clear the
  • 01:00:28
    screen
  • 01:00:30
    we get three four one five two but
  • 01:00:32
    you'll notice brown b
  • 01:00:35
    so a b
  • 01:00:36
    c d
  • 01:00:37
    e
  • 01:00:39
    f
  • 01:00:39
    g h i j k l m n l p q r s
  • 01:00:45
    and it's like that so
  • 01:00:46
    uh let's see
  • 01:00:48
    that was ascending we could do the
  • 01:00:50
    opposite descending from z down to a
  • 01:00:53
    right
  • 01:00:54
    so we save this and run it
  • 01:00:58
    we now have the s k e
  • 01:01:01
    b
  • 01:01:02
    and b and
  • 01:01:04
    let's see
  • 01:01:06
    i don't know how they determine which of
  • 01:01:08
    the browns go first since they're both
  • 01:01:10
    the same
  • 01:01:11
    i would guess it's uh
  • 01:01:13
    numerically but we're descending so i
  • 01:01:15
    don't know
  • 01:01:18
    interesting
  • 01:01:19
    so that's order by relatively simple
  • 01:01:22
    and
  • 01:01:23
    yeah not much to it in the next video
  • 01:01:26
    we'll look at and and or
  • 01:01:32
    okay in this video i want to look at and
  • 01:01:34
    and or and and and or allow us to sort
  • 01:01:36
    of extend the functionality of your
  • 01:01:38
    where clause so
  • 01:01:41
    you know
  • 01:01:42
    let's just come right in here and let's
  • 01:01:44
    go where
  • 01:01:46
    so
  • 01:01:47
    and and or allow you to add more
  • 01:01:49
    conditions to your where clause
  • 01:01:52
    basically to search for more than one
  • 01:01:53
    thing so let's look at our
  • 01:01:57
    data real quick so we've got two of
  • 01:01:59
    these browns right so
  • 01:02:01
    let's search for
  • 01:02:05
    where
  • 01:02:06
    last underscore name
  • 01:02:08
    is like
  • 01:02:11
    and then
  • 01:02:12
    let's go br and then that remember that
  • 01:02:15
    percentage sign
  • 01:02:17
    and
  • 01:02:18
    right and where row id
  • 01:02:23
    well actually first let's just run this
  • 01:02:25
    by itself just to make sure this works
  • 01:02:26
    right so here
  • 01:02:29
    boom we get mary and wes brown now let's
  • 01:02:32
    say we only want mary
  • 01:02:35
    but we're not sure so we have to search
  • 01:02:37
    also so we could go
  • 01:02:40
    where
  • 01:02:42
    last name is like bruh
  • 01:02:44
    and
  • 01:02:47
    row id
  • 01:02:49
    equals three right
  • 01:02:52
    so notice we have two
  • 01:02:55
    two conditions this condition and
  • 01:02:58
    this condition and both of those have to
  • 01:03:01
    be true in order for it to return
  • 01:03:03
    something right so if we save this and
  • 01:03:05
    run it
  • 01:03:06
    we see
  • 01:03:07
    boom just mary brown number three right
  • 01:03:11
    we can also use or
  • 01:03:13
    instead we could go or in this case it's
  • 01:03:17
    going to return two different results
  • 01:03:20
    mary and wes why
  • 01:03:22
    well because this is a different logical
  • 01:03:25
    type of thing before we just wanted we
  • 01:03:28
    wanted and and means both
  • 01:03:30
    or
  • 01:03:31
    means either or or right do this or do
  • 01:03:35
    that so
  • 01:03:37
    this has to be true
  • 01:03:39
    or
  • 01:03:40
    this has to be true and in our case up
  • 01:03:43
    here
  • 01:03:44
    the last name has to start with br
  • 01:03:47
    or
  • 01:03:48
    the number is three well this one starts
  • 01:03:50
    with br its number is not three but it
  • 01:03:53
    doesn't both have to be only one has to
  • 01:03:55
    be true in order for it to return and in
  • 01:03:57
    this case br
  • 01:03:59
    is true for this one so it returns wes
  • 01:04:02
    and mary so
  • 01:04:04
    a
  • 01:04:04
    pretty simple concept but very useful
  • 01:04:07
    there are times when you need to search
  • 01:04:08
    for several different things and i've
  • 01:04:10
    just done
  • 01:04:12
    two things
  • 01:04:14
    one
  • 01:04:15
    two you could do as many as you want
  • 01:04:16
    just keep slapping on
  • 01:04:19
    uh ors or
  • 01:04:21
    or
  • 01:04:24
    you know you get it or
  • 01:04:25
    you know email
  • 01:04:27
    is like
  • 01:04:29
    something or you know and you could just
  • 01:04:31
    keep going and going and going i don't
  • 01:04:32
    know why you would want to but there are
  • 01:04:33
    certain circumstances where you might
  • 01:04:35
    have a few things you want to search for
  • 01:04:37
    you could do that with a work if you
  • 01:04:39
    need all of them to be true use and
  • 01:04:42
    and
  • 01:04:43
    pretty simple in the next video we'll
  • 01:04:45
    look at limit
  • 01:04:50
    okay in the last video we looked at and
  • 01:04:52
    or in this video i will want to look at
  • 01:04:54
    limiting so
  • 01:04:56
    our little database only has five
  • 01:04:58
    records so you know we likely would
  • 01:05:01
    never want to limit the results but in
  • 01:05:03
    reality you might have
  • 01:05:05
    a thousand records ten thousand hundred
  • 01:05:07
    thousand a million records two million
  • 01:05:09
    records
  • 01:05:11
    you know you might only wanna return a
  • 01:05:13
    certain number
  • 01:05:14
    of results right so to do that you use
  • 01:05:18
    limit and it's very simple you just type
  • 01:05:20
    in
  • 01:05:21
    limit
  • 01:05:22
    and then the number limit two right so
  • 01:05:25
    here if we save this we're selecting the
  • 01:05:27
    row id and everything from customers
  • 01:05:30
    limit two
  • 01:05:32
    i
  • 01:05:33
    can't imagine you would guess what the
  • 01:05:35
    result will be for this but
  • 01:05:37
    it limits it to two results right
  • 01:05:40
    so
  • 01:05:41
    we have one and two
  • 01:05:43
    let's see if we can string along some
  • 01:05:46
    let's go uh descending this is going to
  • 01:05:48
    throw an error i think
  • 01:05:51
    i'm not sure so i'm going to run it yeah
  • 01:05:53
    so we got to go you know
  • 01:05:56
    order by
  • 01:05:59
    what row
  • 01:06:02
    id descending right that might work
  • 01:06:09
    no
  • 01:06:12
    we actually have to put our limit at the
  • 01:06:14
    end in this case
  • 01:06:17
    from customers order by row id
  • 01:06:20
    descending limit to
  • 01:06:22
    i think this will work now
  • 01:06:24
    getting sloppy so now we have five and
  • 01:06:26
    four the last two right
  • 01:06:28
    um
  • 01:06:29
    yeah
  • 01:06:31
    i should mention all of these things
  • 01:06:32
    we're learning you can usually just
  • 01:06:34
    string along just by adding them to your
  • 01:06:37
    executing statement here right
  • 01:06:40
    so uh sometimes they have to be in
  • 01:06:41
    specific order like we just saw just now
  • 01:06:45
    order by has to go first and then limit
  • 01:06:47
    it at the end
  • 01:06:49
    if it doesn't work when you try
  • 01:06:50
    something try changing the order and it
  • 01:06:52
    probably will so
  • 01:06:54
    you know
  • 01:06:55
    pretty simple
  • 01:06:57
    limit
  • 01:06:58
    three limit however many you want
  • 01:07:02
    and very cool so
  • 01:07:03
    um that's limit it's very simple
  • 01:07:06
    not a whole lot to it but i thought i
  • 01:07:08
    would mention it just in case you ever
  • 01:07:10
    need the limit and uh yeah so in the
  • 01:07:13
    next video i'll show you how to delete a
  • 01:07:15
    table
  • 01:07:21
    okay so we've already looked at deleting
  • 01:07:23
    a record that's pretty straightforward
  • 01:07:25
    in this video i want to show you how to
  • 01:07:26
    delete an entire table so like i said a
  • 01:07:30
    couple videos ago when we talked about
  • 01:07:31
    deleting when you remove stuff from a
  • 01:07:33
    table you're dropping it from the table
  • 01:07:36
    from the database same thing with a
  • 01:07:38
    table you drop the table from the
  • 01:07:41
    database right so
  • 01:07:43
    to do that we want to c dot execute
  • 01:07:49
    right and we're going to need to
  • 01:07:52
    commit that
  • 01:07:55
    i'll just go ahead and put that right
  • 01:07:56
    there
  • 01:07:57
    now inside of here
  • 01:08:00
    first you take a a deep breath and then
  • 01:08:04
    you just type in drop table
  • 01:08:06
    and then
  • 01:08:08
    the name of the table customers
  • 01:08:10
    all right so if we save this
  • 01:08:12
    it will drop the table and then try and
  • 01:08:18
    output everything from the table which
  • 01:08:20
    there shouldn't be anything in the table
  • 01:08:22
    so
  • 01:08:23
    see how that goes
  • 01:08:26
    and we get an error no such table
  • 01:08:28
    customers
  • 01:08:30
    so that tells us that the
  • 01:08:34
    the table has been dropped it has been
  • 01:08:36
    deleted and like everything else
  • 01:08:39
    when you delete a thing it's gone that
  • 01:08:41
    table is gone it no longer exists
  • 01:08:44
    and uh
  • 01:08:46
    that's it so drop table
  • 01:08:48
    pretty simple
  • 01:08:50
    and uh
  • 01:08:51
    yeah that's how that works so i'm gonna
  • 01:08:53
    rebuild the table after this video and
  • 01:08:55
    we're gonna play around with this stuff
  • 01:08:56
    some more and sort of build a very basic
  • 01:08:59
    uh database app that we can use and
  • 01:09:02
    we'll start to look at that in the next
  • 01:09:03
    video
  • 01:09:08
    okay in the last video we deleted our
  • 01:09:10
    table we dropped it i've gone ahead and
  • 01:09:11
    recreated it i put all the data back in
  • 01:09:13
    exactly as it was so we can continue to
  • 01:09:15
    use it
  • 01:09:16
    in this video i want to start to build
  • 01:09:18
    out a little app and it takes sort of
  • 01:09:20
    all the stuff we've learned up until now
  • 01:09:22
    and sort of uses it so we've at this
  • 01:09:25
    point we've learned all the basics for
  • 01:09:28
    sqlite three with python and you know we
  • 01:09:31
    went through it really quickly but
  • 01:09:32
    there's not a whole lot to it's a very
  • 01:09:33
    simple database to use and uh it's one
  • 01:09:36
    of the nice things about it so
  • 01:09:38
    from here on out we're not going to do
  • 01:09:40
    anything revolutionary we're just going
  • 01:09:41
    to build a simple app that sort of makes
  • 01:09:44
    all the stuff we've done up until now a
  • 01:09:45
    little easier sort of
  • 01:09:48
    splits it apart into a couple of
  • 01:09:49
    different files so it's easier to read
  • 01:09:52
    and should be a lot of fun it'll help us
  • 01:09:53
    reinforce the stuff we've already
  • 01:09:55
    learned and
  • 01:09:56
    i don't know just be kind of fun so i'm
  • 01:09:58
    going to start right off the bat i'm
  • 01:09:59
    going to create a new file and let's
  • 01:10:01
    save this as
  • 01:10:03
    i don't know our app
  • 01:10:06
    dot pi
  • 01:10:10
    and the first thing we want to do is
  • 01:10:12
    import
  • 01:10:14
    database and that's just
  • 01:10:17
    this file right here that we've been
  • 01:10:18
    working on throughout the whole course
  • 01:10:20
    database.pi we can import that into
  • 01:10:22
    another file
  • 01:10:24
    so what we want to do here is
  • 01:10:26
    take all of this stuff and
  • 01:10:29
    build it into little functions python
  • 01:10:31
    functions that we can then call
  • 01:10:33
    from the other file so we'll sort of
  • 01:10:36
    abstract all the database stuff away
  • 01:10:39
    and
  • 01:10:40
    just make everything a lot cleaner so
  • 01:10:41
    i'm going to start out right here i'm
  • 01:10:44
    just going to push all this stuff down
  • 01:10:45
    let's create a function
  • 01:10:47
    and let's go define and let's call
  • 01:10:50
    let's call it show
  • 01:10:52
    all
  • 01:10:53
    right and what we want to do here is
  • 01:10:55
    just show all the stuff in our database
  • 01:10:58
    right in our table so i'm going to take
  • 01:11:00
    all of this stuff
  • 01:11:03
    and i'm just going to paste it inside
  • 01:11:06
    of this function
  • 01:11:10
    and
  • 01:11:13
    paste
  • 01:11:14
    and same thing
  • 01:11:17
    with this
  • 01:11:22
    and
  • 01:11:24
    let's add these guys
  • 01:11:28
    as well
  • 01:11:33
    indenting is important for functions so
  • 01:11:38
    okay and so we can get rid of that
  • 01:11:42
    now
  • 01:11:43
    these two guys i think we need to put
  • 01:11:46
    these in
  • 01:11:48
    each of our functions
  • 01:11:51
    that we create
  • 01:11:56
    okay
  • 01:11:58
    so
  • 01:12:00
    i think that looks good right
  • 01:12:06
    so let's create a comment and let's call
  • 01:12:08
    uh queer let's go query
  • 01:12:11
    quest query
  • 01:12:13
    the
  • 01:12:14
    database
  • 01:12:15
    and
  • 01:12:16
    return
  • 01:12:18
    all
  • 01:12:19
    records
  • 01:12:21
    right so
  • 01:12:23
    here we're connecting to the database
  • 01:12:25
    creating a cursor executing a command to
  • 01:12:28
    show all the stuff in there
  • 01:12:31
    putting it into this variable and then
  • 01:12:33
    looping through that variable and
  • 01:12:34
    printing out each item onto the screen
  • 01:12:36
    and then closing
  • 01:12:38
    making our commit in this case we don't
  • 01:12:39
    actually need to commit anything because
  • 01:12:41
    we're not we're just
  • 01:12:42
    querying we're not committing
  • 01:12:44
    uh so we'll probably take that out
  • 01:12:46
    eventually
  • 01:12:47
    and then at the end we want to close our
  • 01:12:48
    connection so if we save this
  • 01:12:50
    and head back over here
  • 01:12:52
    now if we want to actually use that we
  • 01:12:56
    just
  • 01:12:57
    call
  • 01:12:58
    database dot
  • 01:13:00
    show underscore all
  • 01:13:03
    like you would call any function
  • 01:13:05
    we need to
  • 01:13:06
    put it in this database because the name
  • 01:13:08
    of the file here is database dot
  • 01:13:10
    show all which is the name of that
  • 01:13:12
    function right there
  • 01:13:14
    and we're not passing anything in
  • 01:13:15
    because it doesn't require us to all
  • 01:13:18
    right so if we save this what do we call
  • 01:13:19
    this our app.pi
  • 01:13:21
    and if we head back over here we can go
  • 01:13:23
    python
  • 01:13:26
    our
  • 01:13:29
    app.pi
  • 01:13:30
    if we run it boom
  • 01:13:32
    it prints out everything
  • 01:13:35
    onto the screen very very cool
  • 01:13:38
    now likewise if we commented this out
  • 01:13:41
    and saved this and ran it
  • 01:13:44
    again not that's going to happen
  • 01:13:46
    so
  • 01:13:47
    there's nothing else going on in our r
  • 01:13:49
    app dot pi
  • 01:13:51
    so
  • 01:13:52
    and our database dot pi only has one
  • 01:13:54
    function now we're going to spend the
  • 01:13:56
    rest of the course building out more
  • 01:13:57
    functions that we can then call but
  • 01:14:00
    that's a good way to get started i think
  • 01:14:02
    and the next video will go on and do the
  • 01:14:05
    next thing
  • 01:14:10
    okay so we've created a function to show
  • 01:14:12
    everything now let's create a function
  • 01:14:14
    to add a record
  • 01:14:16
    to our database and so let's call this
  • 01:14:18
    define
  • 01:14:19
    and let's call add one
  • 01:14:22
    right
  • 01:14:25
    and here let's make a comment that says
  • 01:14:28
    add
  • 01:14:29
    a new record to the
  • 01:14:32
    table
  • 01:14:34
    all right
  • 01:14:35
    so we need to pass in
  • 01:14:37
    the stuff that we want to add to the
  • 01:14:39
    table so
  • 01:14:41
    we want first let's call it last
  • 01:14:44
    and email
  • 01:14:46
    right
  • 01:14:47
    so
  • 01:14:48
    we already know
  • 01:14:50
    how to
  • 01:14:51
    add
  • 01:14:54
    stuff to our database what we do is well
  • 01:14:56
    first we need
  • 01:14:58
    these two guys
  • 01:15:02
    we need to create a connection and a
  • 01:15:04
    cursor and since these are inside
  • 01:15:06
    functions you need to do it every time
  • 01:15:09
    connect to the database
  • 01:15:10
    and create cursor let's just get rid of
  • 01:15:14
    this
  • 01:15:15
    there we go
  • 01:15:17
    right
  • 01:15:18
    next we go c dot execute as we already
  • 01:15:21
    know how to do
  • 01:15:23
    and we want to insert
  • 01:15:25
    into and our
  • 01:15:27
    table is customers
  • 01:15:30
    and the values
  • 01:15:33
    are
  • 01:15:34
    we want to put placeholders we learned
  • 01:15:36
    how to do that already
  • 01:15:39
    right
  • 01:15:40
    now
  • 01:15:42
    to add the variables that we passed
  • 01:15:45
    first last in email
  • 01:15:48
    we just put them right here
  • 01:15:50
    first last email
  • 01:15:54
    okay and then we need to commit this
  • 01:15:59
    and close our connection
  • 01:16:02
    right underneath there boom
  • 01:16:05
    commit our command
  • 01:16:06
    close our connection
  • 01:16:08
    all right that looks pretty good
  • 01:16:11
    so if we save this
  • 01:16:13
    and head back over to our app
  • 01:16:16
    now
  • 01:16:17
    to run this guy we just called database
  • 01:16:20
    dot
  • 01:16:22
    add one
  • 01:16:24
    do we call it add one let's make sure
  • 01:16:25
    here
  • 01:16:27
    add one yep now we need to pass it three
  • 01:16:30
    things a first name a last name and an
  • 01:16:31
    email
  • 01:16:32
    so let's go um
  • 01:16:35
    oops wrong one
  • 01:16:37
    here let's go
  • 01:16:39
    well
  • 01:16:41
    first name last name and email
  • 01:16:44
    we might go who
  • 01:16:46
    um give me a name here
  • 01:16:49
    uh laura
  • 01:16:52
    smith
  • 01:16:54
    lara at
  • 01:16:56
    smith.com
  • 01:16:58
    right
  • 01:16:59
    so this will run that and then if we
  • 01:17:01
    want to show it all we'll keep this one
  • 01:17:03
    here
  • 01:17:05
    just to
  • 01:17:08
    to make sure so let's go
  • 01:17:10
    add
  • 01:17:11
    a record
  • 01:17:13
    to the database
  • 01:17:16
    here let's go show all the records
  • 01:17:20
    all right so if we save this and run it
  • 01:17:26
    boom we see laura smith laura at
  • 01:17:28
    smith.com
  • 01:17:30
    has been added
  • 01:17:32
    very cool very easy
  • 01:17:34
    and uh that's all there is to it in the
  • 01:17:36
    next video we will create a function to
  • 01:17:38
    delete a record from our database
  • 01:17:45
    okay in the last video we added a new
  • 01:17:46
    record to the table in this video we
  • 01:17:48
    want to create a function that deletes a
  • 01:17:50
    record from the table and this was a
  • 01:17:51
    little bit trickier than you might
  • 01:17:53
    expect and i'll show you what i'm
  • 01:17:55
    talking about in just a second so let's
  • 01:17:56
    head over here to our database.pi file
  • 01:17:59
    and let's create a
  • 01:18:02
    delete
  • 01:18:03
    one
  • 01:18:07
    function and we want to pass in id
  • 01:18:10
    now inside of here
  • 01:18:12
    again we're gonna
  • 01:18:13
    make our connections here
  • 01:18:16
    and we want to close our connections
  • 01:18:18
    here
  • 01:18:21
    and inside of here
  • 01:18:23
    we want to c dot execute
  • 01:18:27
    all right and we know how to do this
  • 01:18:28
    already we want to
  • 01:18:31
    delete
  • 01:18:32
    delete from customers
  • 01:18:36
    where
  • 01:18:38
    and we want to let's do row id
  • 01:18:41
    equals and here we want to put
  • 01:18:44
    a placeholder
  • 01:18:47
    all right and then
  • 01:18:49
    we want to pass in this id
  • 01:18:52
    which is this id right
  • 01:18:55
    so
  • 01:18:55
    that looks pretty good
  • 01:19:00
    okay so that'll work but now head back
  • 01:19:03
    over to our app to use this thing we
  • 01:19:06
    call database dot what do we call this
  • 01:19:08
    remove
  • 01:19:10
    delete one
  • 01:19:14
    let's copy this dot delete one and now
  • 01:19:18
    we want to pass in the number of the id
  • 01:19:20
    we want to delete in this case we want
  • 01:19:21
    to delete 6.
  • 01:19:23
    it's not up anymore but 6 was the one we
  • 01:19:25
    just added
  • 01:19:27
    in the last video now here's the weird
  • 01:19:29
    thing
  • 01:19:30
    the ids are numbers right integers we
  • 01:19:34
    know they're integers the database knows
  • 01:19:36
    they're integers so you would think we
  • 01:19:39
    would pass an integer but if we do
  • 01:19:41
    we save this and run it
  • 01:19:45
    we get an error
  • 01:19:47
    it says module database has no attribute
  • 01:19:49
    delete one wait that's the wrong error
  • 01:19:52
    uh database delete one did we not save
  • 01:19:56
    let's pull this back up
  • 01:19:59
    delete one
  • 01:20:00
    must not have saved it all right so
  • 01:20:02
    now let's clear the screen run this
  • 01:20:05
    again
  • 01:20:06
    all right this is the different this is
  • 01:20:07
    the the error i thought we would get
  • 01:20:09
    value error parameters are of
  • 01:20:11
    unsupported type for this id
  • 01:20:14
    right we're passing a number an integer
  • 01:20:17
    for some reason we can't do that we need
  • 01:20:19
    to pass
  • 01:20:21
    this six
  • 01:20:23
    as a string
  • 01:20:25
    so let's go delete
  • 01:20:28
    record
  • 01:20:30
    use row id as string right
  • 01:20:36
    so now if we save this in quotation
  • 01:20:38
    marks it'll pass this six
  • 01:20:40
    over
  • 01:20:41
    to here id becomes six a string that
  • 01:20:45
    gets put into there that then somehow
  • 01:20:47
    magically becomes a number again and it
  • 01:20:49
    should work it's the weirdest thing
  • 01:20:53
    if we run it boom now it works six has
  • 01:20:56
    been deleted
  • 01:20:58
    and just that simple so
  • 01:21:00
    that's how you delete stuff
  • 01:21:02
    in the next video i think we already
  • 01:21:04
    looked at how to add a one record let's
  • 01:21:08
    create one to add a bunch of records all
  • 01:21:10
    at once and we'll look at that in the
  • 01:21:12
    next video
  • 01:21:17
    okay so in this video we want to create
  • 01:21:19
    a function to add many records to our
  • 01:21:22
    table we've already done one where we've
  • 01:21:24
    add one record
  • 01:21:26
    so make sense that we would do for many
  • 01:21:28
    so let's go define
  • 01:21:30
    add many
  • 01:21:32
    and let's pass in a list we'll just call
  • 01:21:34
    it list
  • 01:21:36
    and
  • 01:21:37
    we can i'm just going to copy all of
  • 01:21:39
    this stuff
  • 01:21:43
    so
  • 01:21:45
    what we want to do is execute many
  • 01:21:48
    and then insert into customers values
  • 01:21:52
    then here
  • 01:21:53
    we want
  • 01:21:55
    to pass in list which is this guy right
  • 01:21:58
    here
  • 01:21:59
    all right
  • 01:22:00
    does that look good
  • 01:22:02
    i think so so let's save this and head
  • 01:22:04
    back over here
  • 01:22:06
    and we don't want to delete anything so
  • 01:22:08
    here let's go
  • 01:22:10
    add
  • 01:22:13
    many records
  • 01:22:17
    all right so we want to call
  • 01:22:19
    database dot
  • 01:22:23
    add many
  • 01:22:26
    and we want to pass in
  • 01:22:28
    our
  • 01:22:29
    stuff i don't know
  • 01:22:31
    so here we need to define stuff we need
  • 01:22:34
    to create a list
  • 01:22:36
    right
  • 01:22:37
    and
  • 01:22:39
    inside of this
  • 01:22:41
    let's add let's add two records
  • 01:22:43
    all right so here we want
  • 01:22:47
    bump
  • 01:22:49
    bump
  • 01:22:51
    well
  • 01:22:52
    and i'm just going to copy this
  • 01:22:56
    and here let's go brenda
  • 01:23:01
    um
  • 01:23:02
    smitherton
  • 01:23:03
    i don't know
  • 01:23:04
    and she is brenda
  • 01:23:07
    smitherton.com
  • 01:23:11
    and here we'll call joshua
  • 01:23:15
    raintree
  • 01:23:17
    i don't know where i get these names
  • 01:23:18
    josh at
  • 01:23:23
    raintree.com all right so we're going to
  • 01:23:26
    call add many which is
  • 01:23:28
    this guy
  • 01:23:30
    we're going to pass in stuff which is
  • 01:23:32
    all of this
  • 01:23:36
    right
  • 01:23:39
    so stuff which is this list
  • 01:23:41
    becomes
  • 01:23:43
    list
  • 01:23:44
    and then here we just pass that in
  • 01:23:47
    and since we have execute many it should
  • 01:23:49
    work
  • 01:23:51
    i think
  • 01:23:52
    fingers crossed
  • 01:23:55
    okay and then at the end we'll show all
  • 01:23:57
    so all right let's see if this works
  • 01:23:59
    let's clear the screen
  • 01:24:02
    boom
  • 01:24:04
    so we have brenda smitherton and joshua
  • 01:24:07
    raintree number six and number seven
  • 01:24:11
    and it worked just that easy all right
  • 01:24:13
    so that was pretty cool a little bit
  • 01:24:15
    more complicated but
  • 01:24:17
    still not too bad now
  • 01:24:20
    we've sort of done all this stuff in
  • 01:24:22
    this program here and which i think you
  • 01:24:25
    would probably want to do as opposed to
  • 01:24:27
    adding it here somehow
  • 01:24:29
    so that makes sense right
  • 01:24:31
    yeah
  • 01:24:32
    okay so that's all for this video in the
  • 01:24:34
    next video we'll look at
  • 01:24:37
    where
  • 01:24:38
    and uh
  • 01:24:39
    yeah i think that's about
  • 01:24:41
    getting to the end of the stuff that we
  • 01:24:42
    need to add into your as functions so
  • 01:24:45
    yeah look at that in the next video
  • 01:24:51
    okay so let's crank out one more of
  • 01:24:53
    these functions let's look at the where
  • 01:24:55
    clause now
  • 01:24:57
    to create a function to deal with where
  • 01:24:59
    is kind of silly i mean there's just so
  • 01:25:01
    many options you could
  • 01:25:03
    look up you know pull all the
  • 01:25:05
    records from a database where first name
  • 01:25:08
    is x or where last name is x or where
  • 01:25:10
    email address is x or where
  • 01:25:12
    you know row id is x or you know any of
  • 01:25:16
    a zillion different permutations of that
  • 01:25:18
    so it doesn't really make a lot of sense
  • 01:25:20
    to create a where function
  • 01:25:22
    but we're not really building a real app
  • 01:25:24
    here we're just sort of playing around
  • 01:25:26
    with this to reinforce some of the
  • 01:25:27
    concepts so we might as well just knock
  • 01:25:29
    one out and
  • 01:25:31
    let's just say we want to look up email
  • 01:25:33
    addresses it's pretty simple so
  • 01:25:36
    let's just call
  • 01:25:38
    define email
  • 01:25:40
    look up
  • 01:25:43
    and we want to pass email i guess
  • 01:25:46
    all right so i'm just gonna copy all of
  • 01:25:48
    this
  • 01:25:51
    paste it in
  • 01:25:53
    and
  • 01:25:54
    for our execute
  • 01:25:56
    what do we want to do we want to select
  • 01:25:59
    everything from customers right
  • 01:26:03
    where
  • 01:26:04
    and we want where email
  • 01:26:06
    equals
  • 01:26:08
    and we want to pass in
  • 01:26:12
    email
  • 01:26:15
    all right
  • 01:26:16
    now this is a tuple we're passing one
  • 01:26:18
    thing
  • 01:26:19
    and so
  • 01:26:21
    we put a little
  • 01:26:22
    comma afterwards just a little quirk
  • 01:26:25
    and uh
  • 01:26:26
    sort of how that goes now we also
  • 01:26:29
    we're not committing anything here we're
  • 01:26:32
    querying and we want to put it up on the
  • 01:26:34
    screen so we need to come up here
  • 01:26:36
    and let's see
  • 01:26:40
    like a with a query
  • 01:26:41
    we need to
  • 01:26:43
    fetch all
  • 01:26:44
    and let's loop through
  • 01:26:46
    and print those out so i'm just going to
  • 01:26:47
    copy that
  • 01:26:50
    and that looks
  • 01:26:51
    good i think
  • 01:26:55
    yeah
  • 01:26:57
    okay so i think that looks good right
  • 01:27:00
    let's save this and head back over to
  • 01:27:02
    our app and look up email address so we
  • 01:27:04
    want to call database
  • 01:27:06
    dot email
  • 01:27:08
    lookup
  • 01:27:10
    and we want to pass something so let's
  • 01:27:12
    pass john codamy.com we know that's an
  • 01:27:16
    address that exists in our system
  • 01:27:18
    so we can head back over here
  • 01:27:21
    run this again oh first of all
  • 01:27:25
    let's comment out this
  • 01:27:27
    show all one we don't need to show all
  • 01:27:29
    we just want to show
  • 01:27:31
    the one that we're looking up so save
  • 01:27:32
    this
  • 01:27:35
    run it
  • 01:27:36
    oh
  • 01:27:38
    oh damn data nace
  • 01:27:41
    why didn't you tell me i misspelled that
  • 01:27:44
    database all right
  • 01:27:46
    let's clear this screen and run this
  • 01:27:48
    again and boom john elder
  • 01:27:51
    john codamy.com if we want the id
  • 01:27:55
    as well
  • 01:27:56
    so we would go just we could add it like
  • 01:27:58
    this row id and everything
  • 01:28:01
    come back here
  • 01:28:03
    and now we get the row id so you know
  • 01:28:05
    you can do this for anything you want if
  • 01:28:07
    you want to
  • 01:28:08
    create an email lookup one you could do
  • 01:28:10
    that if you wanted to create a specific
  • 01:28:12
    row id you know primary key id lookup
  • 01:28:15
    you could do that like i said this app
  • 01:28:18
    we're building here
  • 01:28:19
    we have built i suppose
  • 01:28:22
    is
  • 01:28:23
    you know this is not supposed to be some
  • 01:28:24
    professional app we're building this is
  • 01:28:26
    just a little toy thing that we can sort
  • 01:28:28
    of play around with just to reinforce
  • 01:28:30
    all of these different things and it's
  • 01:28:32
    kind of nice to have this you know
  • 01:28:35
    examples of how to for instance query
  • 01:28:39
    you know right there how to add
  • 01:28:41
    something to a table you know right
  • 01:28:43
    there how to add many things to a table
  • 01:28:46
    right there how to delete something from
  • 01:28:47
    a table it's just kind of nice to have
  • 01:28:49
    them all in one file
  • 01:28:50
    and
  • 01:28:52
    you know
  • 01:28:55
    for reference going forward into the
  • 01:28:57
    future if you need to refer back to this
  • 01:28:58
    if you've built this with with me along
  • 01:29:00
    with me you'll have this to sort of you
  • 01:29:02
    know reference back so it's kind of cool
  • 01:29:04
    so i think that's all for this video in
  • 01:29:06
    the next video we'll say a few more
  • 01:29:08
    things and wrap this all up okay so
  • 01:29:09
    that's all for this video and for the
  • 01:29:11
    course if you liked it be sure to smash
  • 01:29:13
    the like button below subscribe to the
  • 01:29:14
    channel give me a thumbs up for the
  • 01:29:16
    youtube algorithm and check out my
  • 01:29:17
    website kodamy.com where you can use
  • 01:29:19
    coupon code camp to get 65 off
  • 01:29:21
    membership so you pay just 27 to access
  • 01:29:23
    all my courses over 40 courses hundreds
  • 01:29:26
    of videos in the pdfs of all my
  • 01:29:27
    best-selling coding books join over a
  • 01:29:29
    hundred thousand students learning to
  • 01:29:30
    code just like you my name is john elder
  • 01:29:32
    from codep.com and we'll see in the next
  • 01:29:34
    video
الوسوم
  • coding
  • Python
  • SQLite
  • text editor
  • software setup
  • Git Bash
  • programming tools
  • course introduction
  • John Elder
  • codemy.com