SQLite Create Table
Create a Table
A table stores data in rows and columns, like a spreadsheet. Each column has a name, and each row is one record.
Use the CREATE TABLE statement to create a table. Write the name of the table, and then the columns inside parentheses:
Example
Create a table named players:
cur.execute("""
CREATE TABLE players
(name TEXT, score INTEGER)
""")
Try it Yourself »
The table has two columns, name and score. It has no rows yet. You will add rows in the next chapter.
The examples on this page show only the code for the SQL statements. Click "Try it Yourself" to see the complete code, with import sqlite3 and connect().
Data Types
After each column name, write the data type of the column. The data type tells what kind of data the column holds:
| Data type | Use it for | Example value |
|---|---|---|
| TEXT | Text | 'Emil' |
| INTEGER | Whole numbers | 120 |
| REAL | Decimal numbers | 9.5 |
In SQL, text values are written in single quotes, like 'Emil'.
Note: SQLite is flexible with data types, and does not always stop you from storing text in an INTEGER column. It is still a good idea to choose the right types, so it is clear what each column is for.
Check if a Table Exists
SQLite keeps a list of all the tables in a special table named sqlite_master. Select from it to see the names of your tables:
Example
List the tables in the database:
cur.execute("""
SELECT name FROM sqlite_master
""")
print(cur.fetchall())
Try it Yourself »
Each row in the result is a tuple. This row has only one value, the table name, so the tuple is written with a comma at the end: ('players',).
The Table Already Exists
If you try to create a table that already exists, you get an error:
Example
Create the same table twice:
cur.execute("""
CREATE TABLE players
(name TEXT, score INTEGER)
""")
cur.execute("""
CREATE TABLE players
(name TEXT, score INTEGER)
""")
Try it Yourself »
IF NOT EXISTS
Add IF NOT EXISTS to create the table only if it does not already exist:
Example
No error, even if the table already exists:
cur.execute("""
CREATE TABLE IF NOT EXISTS players
(name TEXT, score INTEGER)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS players
(name TEXT, score INTEGER)
""")
Try it Yourself »
This time there is no error. The second statement does nothing, because the table already exists.
This is useful when the database is saved in a file. The first time the program runs, the table is created. The next times, the table is already there, and nothing happens. You will see this in the SQLite Save to File chapter.
Give Each Row an id
It is often useful to give each row a unique number, so you can find exactly that row later, even if two rows have the same name.
Add a column with the type INTEGER PRIMARY KEY. SQLite then gives each new row the next number automatically:
Example
Create a table with an id column, and add two rows:
cur.execute("""
CREATE TABLE players (
id INTEGER PRIMARY KEY,
name TEXT,
score INTEGER
)
""")
cur.execute("""
INSERT INTO players (name, score)
VALUES ('Emil', 120), ('Tobias', 95)
""")
con.commit()
Try it Yourself »
When a table has many columns, the code is easier to read if you write each column on its own line, like in this example.
The INSERT statement gives values for name and score only, so SQLite fills in the id. You will learn how to insert rows in the next chapter.