SQLite3 is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. In this tutorial, we will learn how to use SQLite3 with Python.
Before we start, make sure you have the sqlite3 library installed in your Python environment.
Importing the library
import sqlite3
Connecting to a database To connect to a database in SQLite, you need to create a Connection object, which represents the database. If the database does not exist, SQLite will create it for you.
conn = sqlite3.connect("example.db")
Creating a cursor object A cursor is used to traverse the records from the result set. A cursor object is created by calling the cursor method of the Connection object.
cursor = conn.cursor()
Creating a table
cursor.execute("CREATE TABLE IF NOT EXISTS employees (id INT PRIMARY KEY, name TEXT, salary REAL)")
Inserting data into a table
cursor.execute("INSERT INTO employees VALUES (1, 'John Doe', 5000)")
cursor.execute("INSERT INTO employees VALUES (2, 'Jane Doe', 6000)")
conn.commit()
Querying data from a table
cursor.execute("SELECT * FROM employees")
employees = cursor.fetchall()
for employee in employees:
print(employee)
Updating data in a table
cursor.execute("UPDATE employees SET salary = 7000 WHERE id = 2")
conn.commit()
Deleting data from a table
cursor.execute("DELETE FROM employees WHERE id = 1")
conn.commit()
Closing the connection
conn.close()
This is a basic tutorial on how to use SQLite3 with Python. With the knowledge of SQL and Python, you can now create more complex applications that interact with databases.