udpate RQ1

This commit is contained in:
Frank Xu
2026-01-31 20:57:45 -05:00
parent 6006957a25
commit 5643228ff6

View File

@@ -1,118 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "346b7f2a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"==================================================\n",
"DATABASE INVENTORY: users4.db\n",
"==================================================\n",
"\n",
"TABLE: users\n",
"COLUMNS (10): user_id, first_name, last_name, email, phone, street, city, state, zip_code, created_at\n",
"--------------------------------------------------\n",
"TABLE: messages\n",
"COLUMNS (4): message_id, sender_id, content, sent_at\n",
"--------------------------------------------------\n",
"\n",
"SUMMARY:\n",
"Total Tables: 2\n",
"Total Columns: 14\n",
"==================================================\n",
"\n"
]
}
],
"source": [
"import sqlite3\n",
"\n",
"def print_database_inventory(db_path: str):\n",
" \"\"\"\n",
" Connects to a SQLite database and prints every table \n",
" along with its associated columns.\n",
" \"\"\"\n",
" try:\n",
" # 1. Connect to the database\n",
" conn = sqlite3.connect(db_path)\n",
" cur = conn.cursor()\n",
"\n",
" # 2. Get all table names\n",
" cur.execute(\"SELECT name FROM sqlite_master WHERE type='table';\")\n",
" tables = [row[0] for row in cur.fetchall()]\n",
"\n",
" print(f\"\\n{'='*50}\")\n",
" print(f\"DATABASE INVENTORY: {db_path}\")\n",
" print(f\"{'='*50}\\n\")\n",
"\n",
" total_cols = 0\n",
"\n",
" # 3. Iterate through each table to find columns\n",
" for table_name in tables:\n",
" # PRAGMA table_info returns (id, name, type, notnull, default_value, pk)\n",
" cur.execute(f\"PRAGMA table_info('{table_name}');\")\n",
" columns = cur.fetchall()\n",
" \n",
" col_names = [col[1] for col in columns]\n",
" total_cols += len(col_names)\n",
"\n",
" # 4. Print the result in a clean format\n",
" print(f\"TABLE: {table_name}\")\n",
" print(f\"COLUMNS ({len(col_names)}): {', '.join(col_names)}\")\n",
" print(\"-\" * 50)\n",
"\n",
" print(f\"\\nSUMMARY:\")\n",
" print(f\"Total Tables: {len(tables)}\")\n",
" print(f\"Total Columns: {total_cols}\")\n",
" print(f\"{'='*50}\\n\")\n",
"\n",
" except sqlite3.Error as e:\n",
" print(f\"Database error: {e}\")\n",
" finally:\n",
" if conn:\n",
" conn.close()\n",
"\n",
"# Usage:\n",
"# print_database_inventory(\"msgstore.db\")\n",
"# Example usage:\n",
"DB_PATH = r\"users4.db\"\n",
"count = print_database_inventory(DB_PATH)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f200703",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}