Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There are many database choices available for Python. For our purposes, we're going to be using SQLite3 for a number of reasons. First of all, SQLite3 comes with Python. If you have Python installed and you're watching this course, then you have SQLite3 and that makes it easy for us for the purposes of teaching. SQLite3 is a perfect choice for a lot of applications. For most of the web site work that I do these days, I'm using SQLite3 instead of MySQL which I might have used a few years ago. It's reliable, it's simple, it doesn't require a separate database engine, it's self-contained, server less, zero configuration and fully transactional.
It's a fully capable database engine in a driver and that makes it just incredibly easy to use. For our purposes, having something that's simple like this allows me to show you how databases work with Python without having to mess with the database a whole lot and spend our time in the Python. So, I'm going to go ahead and start by making a working copy of databases.py. Call it databases-working.py. Go ahead and load that up. You see our first line here says import sqlite3 and that simply imports the Python Library that supports SQLite3.
And then here all we do is we say db = SQLite3.connect and the name of the file, test.db. And we save this and run it and we have created a database. We'll go ahead and refresh this file system here because Eclipse doesn't do that for us and you see that we now have empty file and we've created our database. So, we'll go ahead and populate the database and that's also very simple. We simply say db.execute and I'm going to give it some SQL here and say drop table if exists so that we can create a new table each time and call the table test.
And then db.execute, create table tees, and give it a couple of fields. t1, it's a text field and i1 is an int and I've created a table inside my database. Now I'll just insert a little bit of data. db.execute insert into test (t1, i1, values (?, ?) and these are placeholders and that allows us to give it a tuple with the values one and 1.
And I'll just make a few of these. Two and give that a 2. And a 3 and a 4. And 4 and a three and we have now inserted some data into our database. So this is all done with SQL and we'll say db.commit() because SQLite is a transactional database. It will buffer these values, in case you're going to be using it in a transactional mode. And then I say cursor = db.execute and select star from test order by t1.
Again, standard SQL for row in cursor: print(row). So, now, I've inserted some data into the database and I'm going to print that data out from the database. Save it and run it and there we have it. There's our four records and they're sorted by the t1 field ,which is our text field, and that's getting read from the database. So, that's all there is to it. Using SQLite in Python is incredibly simple. Our first line here connects to the database and that actually creates the file if the file didn't already exist.
And then, from there on, we're using db.execute because db is the database object that we got back from the connect statement and we simply interact with the database using SQL. Be sure to do a commit after you change any data in the database and then we can do a select and use the cursor object that's returned by db.execute and simply step through the cursor object as an iterator and print the data. The data comes back, you'll notice, in tuples and they're in the order that you specify things in your SQL.
So, if I want it in the i1 first followed by t1, I simply change my SQL so that specifies an order, because the order that was in was the order that we define them in the create table text and then int. So, if I want int and then text, I simply change in my SQL and it'll come back in that order and obviously if I want to order it by the integer instead of by the text, I simply change my SQL and it comes ordered by the integer instead of by the text. There's one more thing I'd like to show you in this context and that is the row factory that comes with SQLite.
The SQLite interface is actually incredibly rich and very full featured in Python and there is a lot of options and a lot of methods that you can override and a tremendous amount of power there for working with databases. We are going to keep it simple for our purposes here but there is one thing that I want to show you and that's what's called a row factory. We'll say db.row_factory = sqlite3.Row.
So, what the row factory does is it allows you to specify how rows will be returned from the cursor and the built-in row factory that's provided, sqlite3.Row, is very powerful and very suitable for most purposes. So, when I save this and run it, you'll notice the only change I made was to add that row factory there after the connect. You'll notice now we get row objects instead of those tuples and the row objects can be looked at as tuples if we'd like or it can be looked at as dictionaries, which I find particularly useful.
So, if I say dictionary like that, I'm creating a dictionary object based on a iterable because row is an iterable. So, if I save this and run it, now I get dictionary objects and so they are completely indexed. If I want to, I can say, rows up t1 and I'll get my t1 objects. I can say row.t1, row.i1 and get all the data. Save that and run it.
The built-in row factory from SQLite is very flexible. I tend to use it in the dictionary mode because I find that very convenient, but it has a lot of other options and they're all documented on the SQLite3 page in the Python documentation. So, as you can see, accessing a database from Python is very simple. Most databases have an interface very similar to this one. For most simple database applications, SQLite3 is going to be a great choice and you can see that it's very simple to use in Python.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96929 Viewers
56 Video lessons · 110610 Viewers
71 Video lessons · 79436 Viewers
131 Video lessons · 38119 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.