Python SQLite
Why Use a Database?
When a Python program ends, its variables are gone. If you want to keep data until the next time the program runs, like high scores, notes or a to-do list, you have to save it somewhere.
A database is a place to save data. It also makes it easy to find, sort and change the data later.
What is SQLite?
SQLite is a small database that keeps all its data in a single file.
- SQLite is built into Python, in the
sqlite3module, so there is nothing to install - There is no database server to set up
- SQLite is used in phones, web browsers and many other programs
To work with the database, you use SQL, the language of databases. You do not need to know SQL to start. This tutorial explains each SQL statement when we use it.
Tip: To learn more about SQLite and SQL, visit our SQLite Tutorial. It has a SQL editor that runs SQLite in your browser.
Your First Database
This example creates a database with a table, adds a row to the table, and reads the row back:
Example
Create a table, insert a row, and select it:
import sqlite3
con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("""
CREATE TABLE players
(name TEXT, score INTEGER)
""")
cur.execute("""
INSERT INTO players
VALUES ('Emil', 120)
""")
con.commit()
cur.execute("SELECT * FROM players")
print(cur.fetchall())
Try it Yourself »
Example Explained
import sqlite3imports the sqlite3 module.sqlite3.connect()opens a database, and returns a connection. We store it in a variable namedcon.con.cursor()creates a cursor,cur. You use the cursor to run SQL statements.cur.execute()runs one SQL statement.con.commit()saves the changes in the database.cur.fetchall()returns the rows from theSELECTstatement, as a list.
The SQL statements are written in triple quotes, as multiline strings, so they can be split over many lines.
These are the three SQL statements in the example:
| Statement | What it does |
|---|---|
| CREATE TABLE | Creates a table named players, with the columns name and score |
| INSERT INTO | Adds a row to the table |
| SELECT | Reads rows from the table. The * means all columns |
You will learn more about each statement in the next chapters.
Memory or File?
The special name ":memory:" creates the database in the computer's memory, not in a file. Every time you run the program, you get a new, empty database, and when the program ends, the database is gone.
This is perfect for learning, because every example starts fresh. That is why most examples in this tutorial use ":memory:".
But it also means that every example has to create the table and add the rows again. So in the next chapters, the examples on the page show only the part of the code that the chapter is about. Click "Try it Yourself" to see the complete code.
To keep the data, write a file name instead of ":memory:":
Example
Save the database in a file named game.db:
con = sqlite3.connect("game.db")
If the file does not exist, Python creates it. You will learn more in the SQLite Save to File chapter.
Tip: You can find a list of sqlite3 classes and methods in our Python sqlite3 Module Reference.