BeginnerCoder
beginner#SQLite#Python#Databases

Getting Started with SQLite Databases

This article was AI-drafted and has not been reviewed yet. Spotted a mistake? Tell us.

Learn the basics of SQLite databases and how to interact with them using Python.

Introduction to SQLite

SQLite is a self-contained, file-based database system that allows you to store and manage data in a structured way. To get started with SQLite, you'll need to install the sqlite3 module in Python. You can do this by running pip install pysqlite3 in your terminal.

Creating a Database

To create a new SQLite database, you can use the following code:

import sqlite3
conn = sqlite3.connect('example.db')
cursor = conn.cursor()

This will create a new database file called example.db in the current working directory.

Creating a Table

Once you have a database, you can create a table using the following code:

cursor.execute '''
CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT NOT NULL
)
'''```
This will create a new table called `users` with three columns: `id`, `name`, and `email`.
## Inserting Data
To insert data into the `users` table, you can use the following code:
```python
cursor.execute '''
INSERT INTO users (name, email)
VALUES ('John Doe', 'john@example.com')
'''```
This will insert a new row into the `users` table with the name `John Doe` and email `john@example.com`.
Keep going with SQLite

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.

Getting Started with SQLite Databases — BeginnerCoder