Python & Databases
The Hacker’s Data Vault
Imagine you’re a hacker storing stolen credentials or secret logs. Keeping them in plain text files is risky and inefficient. Instead, you want a vault - a structured, secure place where data can be stored, queried, and managed. That vault is a database.
Python makes it easy to connect to databases like SQLite (lightweight, file‑based) and MySQL (powerful, server‑based). This chapter shows how to open the vault, store secrets, and retrieve them efficiently.
Why Databases Matter
- Structured Storage: Data is organized into tables with rows and columns.
- Efficiency: Queries retrieve exactly what you need, fast.
- Scalability: Databases handle massive datasets better than flat files.
- Security: Access control and transactions protect sensitive information.
- Real‑World Analogy: Like a hacker’s filing cabinet which organized, indexed, and locked.
SQLite – Lightweight & File‑Based
import sqlite3
# Connect to database (creates file if not exists)
conn = sqlite3.connect("hacker.db")
cursor = conn.cursor()
# Create table
cursor.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, password TEXT)")
# Insert data
cursor.execute("INSERT INTO users (name, password) VALUES (?, ?)", ("Shubham", "secret123"))
# Query data
cursor.execute("SELECT * FROM users")
print(cursor.fetchall())
conn.commit()
conn.close()
- Why? SQLite is perfect for small projects but no server required, just a file.
MySQL – Server‑Based & Scalable
import mysql.connector
# Connect to MySQL server
conn = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="hacker_db"
)
cursor = conn.cursor()
# Create table
cursor.execute("CREATE TABLE IF NOT EXISTS logs (id INT AUTO_INCREMENT PRIMARY KEY, event VARCHAR(255))")
# Insert data
cursor.execute("INSERT INTO logs (event) VALUES (%s)", ("Login attempt",))
# Query data
cursor.execute("SELECT * FROM logs")
for row in cursor.fetchall():
print(row)
conn.commit()
conn.close()
- Why? MySQL is ideal for larger, multi‑user systems with complex queries and scalability needs.
SQL Basics Recap
- CREATE TABLE: Define structure.
- INSERT INTO: Add records.
- SELECT: Retrieve data.
- UPDATE: Modify records.
- DELETE: Remove records.
Real‑World Example
import sqlite3
conn = sqlite3.connect("vault.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS creds (user TEXT, password TEXT)")
cursor.execute("INSERT INTO creds VALUES (?, ?)", ("admin", "1234"))
cursor.execute("SELECT * FROM creds WHERE user=?", ("admin",))
print("Admin credentials:", cursor.fetchone())
conn.commit()
conn.close()
- Why? Databases let you store and query sensitive data securely, instead of relying on fragile text files.
The Hacker’s Notebook
- Databases organize data into tables for efficient storage and retrieval. SQLite is lightweight and file‑based, perfect for small projects.
- MySQL is server‑based, scalable, and suited for larger systems. SQL commands (
CREATE,INSERT,SELECT,UPDATE,DELETE) form the foundation of database operations.
Hacker’s Mindset: treat databases as your data vaults. They keep secrets safe, structured, and instantly accessible.

Updated on Jan 3, 2026