How to add an entry to an SQLite database

2 min

Adding entries to an SQLite3 database is very easy.

Here is the code

We connect to the database

import sqlite3

# We create the connection
con = sqlite3.connect('example.db')

# We create a cursor to perform our requests
cur = con.cursor()

# Create a table
cur.execute("CREATE TABLE revenues (date text, amount float, source text)")

One entry

# Here is an example
cur.execute("INSERT INTO table VALUES ('value1', 13124.3, '2020-01-01')")
# Here is a real example
cur.execute("INSERT INTO revenues VALUES ('2020-10-31', 11234.3 ,'shopify')")
Here is a real example

Many entries at once

# Insert multiple entries in the database
entries = [
    ('2020-11-31', 12349.2, 'shopify'),
    ('2020-12-31', 14523.2, 'shopify'),
    ('2021-01-31', 18029.4, 'shopify'),
    ('2021-02-29', 51023.3, 'shopify'),
    ('2021-03-31', 94400.3, 'shopify'),
]
cur.executemany("INSERT INTO revenues VALUES (?,?,?)", entries)

Then

# Save (commit) the changes
con.commit()

Here you are! You now know how to add entries to an SQLite3 Database.

You might be interested in

How to install SQLite3 in Python
Learn how to install SQLite 3 in Python
How to create an SQLite3 Database using Python
Learn how to create an SQLite3 database using Python.
How to connect to an SQLite3 database using Python
Learn how to connect to an SQLite3 database using python.
How to add an entry to an SQLite database
Learn how to add an entry to an SQLite database using the execute() method.