DEV Community

Cover image for Working with Databases in Python: SQLite and PostgreSQL ๐Ÿ“Š๐Ÿ
Info general Hazedawn
Info general Hazedawn

Posted on

1 1 1 1

Working with Databases in Python: SQLite and PostgreSQL ๐Ÿ“Š๐Ÿ

Databases are essential for storing and managing data in applications. Python provides robust libraries for interacting with databases, making it easy to connect, insert, update, and query data. In this blog post, we will explore how to work with SQLite and PostgreSQL in Python, complete with examples for handling user data.

Why Use Databases? ๐Ÿ”‘
Databases allow applications to store data persistently, enabling:

  • Data Management: Efficiently organize and retrieve data. vScalability: Handle large volumes of data without performance degradation.
  • Concurrency: Allow multiple users to access and modify data simultaneously.

Setting Up Your Environment ๐Ÿ› ๏ธ
Before we dive into the code, ensure you have the necessary libraries installed. For SQLite, you donโ€™t need to install anything extra as it comes with Python. For PostgreSQL, youโ€™ll need the psycopg2 library.
Install psycopg2 for PostgreSQL:

bash
pip install psycopg2
Enter fullscreen mode Exit fullscreen mode

Working with SQLite ๐Ÿ“š
Step 1: Connecting to an SQLite Database
Letโ€™s create a simple SQLite database to manage user data. Create a file named sqlite_example.py:

import sqlite3

# Connect to SQLite database (or create it if it doesn't exist)
conn = sqlite3.connect('users.db')
cursor = conn.cursor()

# Create a table for users
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username TEXT NOT NULL,
    email TEXT NOT NULL UNIQUE
)
''')

conn.commit()

Step 2: Inserting User Data
Add the following code to insert user data into the database:
python
def insert_user(username, email):
    cursor.execute('INSERT INTO users (username, email) VALUES (?, ?)', (username, email))
    conn.commit()
    print(f"User {username} added successfully.")

# Example usage
insert_user('john_doe', 'john@example.com')
insert_user('jane_doe', 'jane@example.com')
Enter fullscreen mode Exit fullscreen mode

Step 3: Querying User Data
To retrieve user data, add this function:

def get_users():
    cursor.execute('SELECT * FROM users')
    return cursor.fetchall()

# Example usage
users = get_users()
print("User Data:")
for user in users:
    print(user)
Enter fullscreen mode Exit fullscreen mode

Step 4: Updating User Data
To update user information, use the following function:

def update_user(user_id, new_email):
    cursor.execute('UPDATE users SET email = ? WHERE id = ?', (new_email, user_id))
    conn.commit()
    print(f"User ID {user_id} updated successfully.")

# Example usage
update_user(1, 'new_john@example.com')
Enter fullscreen mode Exit fullscreen mode

Step 5: Closing the Connection
Finally, donโ€™t forget to close the database connection when done:

conn.close()

Enter fullscreen mode Exit fullscreen mode

Working with PostgreSQL ๐Ÿš€

Step 1: Connecting to a PostgreSQL Database

  • Create a file named postgres_example.py and set up your connection:
import psycopg2

# Connect to PostgreSQL database
conn = psycopg2.connect(
    dbname='your_db_name',
    user='your_username',
    password='your_password',
    host='localhost'
)
cursor = conn.cursor()

# Create a table for users
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL UNIQUE
)
''')

conn.commit()

Enter fullscreen mode Exit fullscreen mode

Step 2: Inserting User Data
Add the followpythonng function to insert user data into PostgreSQL:

def insert_user(username, email):
    cursor.execute('INSERT INTO users (username, email) VALUES (%s, %s)', (username, email))
    conn.commit()
    print(f"User {username} added successfully.")

# Example usage
insert_user('john_doe', 'john@example.com')
insert_user('jane_doe', 'jane@example.com')
Enter fullscreen mode Exit fullscreen mode

Step 3: Querying User Data
To retrieve user data from PostgreSQL:


def get_users():
    cursor.execute('SELECT * FROM users')
    return cursor.fetchall()

# Example usage
users = get_users()
print("User Data:")
for user in users:
    print(user)

Enter fullscreen mode Exit fullscreen mode

Step 4: Updating User Data
To update user information in PostgreSQL:

def update_user(user_id, new_email):
    cursor.execute('UPDATE users SET email = %s WHERE id = %s', (new_email, user_id))
    conn.commit()
    print(f"User ID {user_id} updated successfully.")

# Example usage
update_user(1, 'new_john@example.com')
Enter fullscreen mode Exit fullscreen mode

Step 5: Closing the Connection
As with SQLite, make sure to close your PostgreSQL connection:


conn.close()

Enter fullscreen mode Exit fullscreen mode

Conclusion: Mastering Database Operations in Python ๐ŸŽ‰
Working with databases in Python using SQLite and PostgreSQL is straightforward and powerful. By leveraging libraries like sqlite3 and psycopg2, you can efficiently manage user data through CRUD operationsโ€”creating tables, inserting records, querying data, and updating entries.

Next Steps:

  • Explore advanced queries and joins for more complex data retrieval. Implement error handling for better robustness.
  • Consider using an ORM like SQLAlchemy for more abstraction and ease of use.

Start building your database-driven applications today! ๐Ÿ’กโœจ

Python #SQLite #PostgreSQL #DataManagement #WebDevelopment #Database #Coding #TechForBeginners #DataScience #SoftwareEngineering

Image of Timescale

๐Ÿš€ pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applicationsโ€”without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post โ†’

Top comments (0)

Sentry image

See why 4M developers consider Sentry, โ€œnot bad.โ€

Fixing code doesnโ€™t have to be the worst part of your day. Learn how Sentry can help.

Learn more