DATABASE BROWSER
Introduction
Database browsing is an essential task in data management, analysis, and application
development. Python provides powerful libraries that facilitate interaction with databases,
enabling efficient data retrieval, manipulation, and storage. This report outlines the key libraries
and basic examples of how to use them for database browsing in Python.
Key Python Libraries for Database Browsing
1. SQLite3
2. Pandas
3. SQLAlchemy
1. SQLite3
SQLite3 is a lightweight, disk-based database that doesn’t require a separate server process. It is
suitable for small to medium-sized applications, testing, and prototyping.
Example:
import sqlite3
# Connect to a database (or create it)
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
# Create a table
cursor.execute('''CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age
INTEGER)''')
# Insert data
cursor.execute('''INSERT INTO users (name, age) VALUES ('Alice', 30)''')
conn.commit()
# Query data
Department of ECE, EPCET Page 1
DATABASE BROWSER
cursor.execute('''SELECT * FROM users''')
print(cursor.fetchall())
# Close the connection
conn.close()
2. Pandas
Pandas is a powerful data manipulation library that can read from and write to various database
formats. It is ideal for data analysis and manipulation.
Example:
import pandas as pd
import sqlite3
# Connect to a database
conn = sqlite3.connect('example.db')
# Read data into a DataFrame
df = pd.read_sql_query('SELECT * FROM users', conn)
print(df)
# Write DataFrame to a new table
df.to_sql('users_copy', conn, if_exists='replace', index=False)
# Close the connection
conn.close()
Department of ECE, EPCET Page 2
DATABASE BROWSER
3. SQLAlchemy
SQLAlchemy is an SQL toolkit and Object-Relational Mapping (ORM) library. It provides a
high-level API for database interactions and supports multiple database types.
Example:
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Define the SQLite database
engine = create_engine('sqlite:///example.db')
Base = declarative_base()
# Define a User model
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
age = Column(Integer)
# Create the table
Base.metadata.create_all(engine)
# Create a session
Session = sessionmaker(bind=engine)
session = Session()
Department of ECE, EPCET Page 3
DATABASE BROWSER
# Add a new user
new_user = User(name='Bob', age=25)
session.add(new_user)
session.commit()
# Query users
users = session.query(User).all()
for user in users:
print(user.name, user.age)
# Close the session
session.close()
Conclusion:
Python provides a variety of libraries for database browsing, each suited for different types of
databases and use cases. SQLite3 is excellent for lightweight local databases, Pandas is great for
data manipulation, SQLAlchemy offers powerful ORM capabilities, and PyMySQL is ideal for
MySQL interactions. By leveraging these libraries, users can efficiently manage, query, and
analyze data stored in databases.
References:
SQLite3 Documentation
Pandas Documentation
SQLAlchemy Documentation
Department of ECE, EPCET Page 4