beginner#SQLite#Python#Databases
Getting Started with SQLite Databases
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`.