{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cb7393db-279a-40b4-825c-7628c7f4161b",
   "metadata": {},
   "source": [
    "# Fetch Records from a Database, *cont'd*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c2769b7-ab08-4ad8-af36-6257935e2508",
   "metadata": {},
   "outputs": [],
   "source": [
    "from data201 import db_connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b0c66ef-fea8-47ed-a4d1-87822c5c3688",
   "metadata": {},
   "outputs": [],
   "source": [
    "conn = db_connection('school.ini')\n",
    "cursor = conn.cursor()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "112f04ac-08c5-44e2-98e4-c898a159e4bd",
   "metadata": {},
   "source": [
    "#### Use `cursor.fetchone()` to **fetch one row** at a time. If there are no more rows, `None` is returned."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59378ef7-95f3-492c-a567-da27082577ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = \"SELECT * FROM class\";\n",
    "\n",
    "cursor.execute(sql)\n",
    "row = cursor.fetchone()\n",
    "\n",
    "index = 1\n",
    "\n",
    "while row != None:\n",
    "    print(\"-----\")\n",
    "    print(f'{index}: {row}')\n",
    "    row = cursor.fetchone()\n",
    "    index += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0551ffae-248f-456b-a3aa-14e447365dd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d39700ed-d498-4baa-8013-dbe6aa1b8aff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copyright (c) 2025 by Ronald Mak"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
