#100DaysOfCode in Python Transcripts
Chapter: Days 79-81: Basic Database Access with SQLite3
Lecture: Inserting data into the address book
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Time to finally input some data
0:02
into that address book database, okay.
0:05
So I've just opened up a Python shell
0:07
within that same directory, and the first thing
0:10
we'd need to do is import sqlite3.
0:13
Alright, we need to do that every time we invoke the shell.
0:16
Next, we need to open that connection to the database.
0:21
So, we'll do that, same style as before.
0:23
Connection=SQLite3.connect
0:26
addressbook.db
0:29
Make sure you get the name of the database correct,
0:33
because again it will just generate another database
0:36
if you don't connect exactly
0:39
to the same name as you're trying to, okay.
0:42
Next, we need to actually create that cursor
0:45
that allows us to interact with the database.
0:47
So, c=connection.cursor
0:52
And now we get to execute the code.
0:56
So, execute, what are we inserting,
0:59
well what are we doing, rather, we're inserting data into
1:06
our details table within the address book database
1:10
okay, and this is all SQL now.
1:13
So the values that we're putting in there,
1:16
now visualize this from left to right
1:18
column zero and onwards.
1:20
Column zero was name, column one was address,
1:24
and column two was the phone number.
1:27
So, the first column is going to be, my name.
1:32
The second column is going to be my address.
1:36
I promise this is correct.
1:38
And, my last column is going to be my phone number.
1:44
Don't call me after 9:00.
1:46
And, once we're done, we close off
1:49
the actual execute, the SQL.
1:52
And bang we get that nice little return message.
1:55
Now, that would normally be if you were, sort of,
1:59
using that in a script within a With statement or whatnot
2:02
but, we're doing this all manually
2:04
so we actually need to commit our
2:09
session here.
2:10
So, connection.commit,
2:13
now it's actually saved to the database.
2:16
Alright, so let's close, connection.close.
2:20
Alright, let's bring up our database,
2:23
now I already have this open within SQLite browser,
2:27
so lets refresh here using these little funky arrows.
2:31
And there's our data.
2:33
Let's expand that out so you can see it.
2:35
Now, we've got Julian 123 Fake St.
2:39
and my phone number, alright.
2:41
And that's it, that's how we pop data into there
2:43
one line at a time, in a very manual method.