#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 into that address book database, okay. So I've just opened up a Python shell
0:08 within that same directory, and the first thing we'd need to do is import sqlite3. Alright, we need to do that every time we invoke the shell.
0:17 Next, we need to open that connection to the database. So, we'll do that, same style as before. Connection=SQLite3.connect addressbook.db
0:30 Make sure you get the name of the database correct, because again it will just generate another database if you don't connect exactly
0:40 to the same name as you're trying to, okay. Next, we need to actually create that cursor that allows us to interact with the database.
0:48 So, c=connection.cursor And now we get to execute the code. So, execute, what are we inserting,
1:00 well what are we doing, rather, we're inserting data into our details table within the address book database okay, and this is all SQL now.
1:14 So the values that we're putting in there, now visualize this from left to right column zero and onwards. Column zero was name, column one was address,
1:25 and column two was the phone number. So, the first column is going to be, my name. The second column is going to be my address.
1:37 I promise this is correct. And, my last column is going to be my phone number. Don't call me after 9:00. And, once we're done, we close off
1:50 the actual execute, the SQL. And bang we get that nice little return message. Now, that would normally be if you were, sort of,
2:00 using that in a script within a With statement or whatnot but, we're doing this all manually so we actually need to commit our session here.
2:11 So, connection.commit, now it's actually saved to the database. Alright, so let's close, connection.close. Alright, let's bring up our database,
2:24 now I already have this open within SQLite browser, so lets refresh here using these little funky arrows. And there's our data.
2:34 Let's expand that out so you can see it. Now, we've got Julian 123 Fake St. and my phone number, alright.
2:42 And that's it, that's how we pop data into there one line at a time, in a very manual method.


Talk Python's Mastodon Michael Kennedy's Mastodon