{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Database created and connected successfully!\n" ] } ], "source": [ "import sqlite3\n", "\n", "try:\n", " # Connect to (or create) myDB.db\n", " conn = sqlite3.connect('myDB.db')\n", "\n", " print(\"Database created and connected successfully!\")\n", "\n", " # Commit changes and close\n", " conn.commit()\n", " conn.close()\n", "\n", "except sqlite3.Error as e:\n", " print(f\"An error occurred: {e}\")\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 'Bob', 'pka', 'Developer', 'bob@gmail.com')\n", "(2, 'Bob', 'pka', 'Developer', 'bob@gmail.com')\n" ] } ], "source": [ "import sqlite3\n", "\n", "conn = sqlite3.connect('myDB.db')\n", "cursor = conn.cursor()\n", "\n", "cursor.execute('''CREATE TABLE IF NOT EXISTS staff (\n", " id INTEGER PRIMARY KEY AUTOINCREMENT,\n", " name TEXT,\n", " position TEXT,\n", " office TEXT, \n", " email TEXT\n", ")''')\n", "\n", "cursor.execute(\"INSERT INTO staff (name, position, office, email) VALUES (?, ?, ?,?)\",\n", " ('Bob', 'Developer', 'pka','bob@gmail.com'))\n", "\n", "conn.commit()\n", "\n", "for row in cursor.execute('SELECT * FROM staff'):\n", " print(row)\n", "\n", "conn.close()" ] } ], "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.12.4" } }, "nbformat": 4, "nbformat_minor": 2 }