טבלאות, SQL בסיסי, sqlite3 ושאילתות בטוחות. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Tables, basic SQL, sqlite3 and safe queries. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
למהWhy
CREATECREATE
SELECTSELECT
??
שינוייםChanges
GROUP BYGROUP BY
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
למהWhy
למה מסד נתונים?Why a database?
JSON טוב לקצת נתוניםJSON is fine for a little data
מסד: חיפוש מהיר, עדכון של שורה אחתA database: fast search, update one row
SQLite מובנה בפייתון: קובץ אחדSQLite is built into Python: one file
קובץ JSON טוב למדף של 20 ספרים. אבל עם 20,000, כל חיפוש דורש לטעון הכול, וכל שינוי דורש לכתוב הכול מחדש. מסד נתונים שומר טבלאות ויודע לחפש, למיין ולעדכן שורה אחת ביעילות. SQLite מגיע עם פייתון, בלי התקנה, ושומר הכול בקובץ אחד.A JSON file is fine for a 20-book shelf. But with 20,000, every search means loading everything, and every change means rewriting it all. A database stores tables and searches, sorts and updates a single row efficiently. SQLite comes with Python, nothing to install, and keeps everything in one file.
אנלוגיהAnalogy
JSON הוא כמו מחברת: נוח לרשימה קצרה. מסד נתונים הוא כמו קטלוג בספרייה עירונית: עם אינדקס, מגירות ומספרים, כדי למצוא ספר אחד מתוך מאה אלף בשנייה.JSON is like a notebook: handy for a short list. A database is like a city library's catalogue: indexes, drawers and numbers, to find one book among a hundred thousand in a second.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 2
CREATECREATE
טבלה ושורותTables and rows
CREATE TABLE עם עמודות וטיפוסיםCREATE TABLE with columns and types
SQL היא שפה נפרדת שכותבים בתוך מחרוזות. CREATE TABLE מגדיר עמודות: TEXT, INTEGER, REAL. המפתח הראשיid מקבל מספר חדש בכל הכנסה. executemany מכניס רשימה שלמה בבת אחת. מוסכמה: מילות SQL באותיות גדולות, כדי להבדיל אותן מהשמות שלנו.SQL is a separate language written inside strings. CREATE TABLE defines columns: TEXT, INTEGER, REAL. The primary keyid gets a fresh number on each insert. executemany inserts a whole list at once. Convention: SQL keywords in capitals, to tell them apart from our names.
אנלוגיהAnalogy
CREATE TABLE הוא כמו לשרטט את הכותרות של טבלה במחברת לפני שממלאים: מחליטים פעם אחת מה העמודות, וכל שורה מתאימה להן.CREATE TABLE is like ruling the headings of a table in a notebook before filling it: decide the columns once, and every row fits them.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 3
SELECTSELECT
SELECT: לשאולSELECT: asking
WHERE מסנן, ORDER BY ממיין, LIMIT חותךWHERE filters, ORDER BY sorts, LIMIT trims
LIKE '%dahl%' לחיפוש חלקיLIKE '%dahl%' for partial search
fetchone / fetchallfetchone / fetchall
SELECT הוא הלב של SQL: אילו עמודות, מאיזו טבלה, ואילו שורות. WHERE הוא כמו if, ORDER BY הוא כמו sorted, ו-LIMIT הוא כמו חיתוך. LIKE עם % מחפש חלק ממחרוזת (בלי רגישות לאותיות באנגלית).SELECT is the heart of SQL: which columns, from which table, and which rows. WHERE is like if, ORDER BY is like sorted, and LIMIT is like slicing. LIKE with % searches part of a string (case-insensitive for English).
אנלוגיהAnalogy
SELECT הוא כמו לבקש מספרן: "תביא לי רק כותרות ועמודים, רק של ספרים מעל 300 עמודים, מהעבה לדקה".SELECT is like asking a librarian: "bring me only titles and pages, only books over 300 pages, thickest first".
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
??
? בטוח מול injectionSafe ? and injection
לעולם לא f-string בתוך SQLNever an f-string inside SQL
כשערך מגיע מהמשתמש, מכניסים אותו עם `?` וטאפל של ערכים, ו-SQLite מטפל בו כנתון בלבד. אם מדביקים קלט עם f-string, קלט זדוני יכול לשנות את השאילתה עצמה: זה SQL injection, אחת הפרצות הנפוצות בעולם. בדוגמה, אותו קלט משפיע אחרת לגמרי בשתי הדרכים:When a value comes from the user, insert it with `?` and a tuple of values, and SQLite treats it purely as data. If you paste input with an f-string, malicious input can change the query itself: that's SQL injection, one of the world's most common holes. In the example, the same input behaves completely differently in the two ways:
אנלוגיהAnalogy
? הוא כמו טופס עם שדות נעולים: מה שכותבים בשדה "שם" נשאר שם, ולא יכול פתאום להפוך להוראה לפקיד.? is like a form with locked fields: whatever is written in the "name" box stays a name and can't suddenly become an instruction to the clerk.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
שינוייםChanges
UPDATE, DELETE ו-commitUPDATE, DELETE and commit
UPDATE ... SET ... WHEREUPDATE ... SET ... WHERE
DELETE FROM ... WHEREDELETE FROM ... WHERE
with con: = commit אוטומטיwith con: = automatic commit
UPDATE משנה שורות קיימות, DELETE מוחק. תמיד עם WHERE: בלעדיו, השינוי חל על כל הטבלה. במסד בקובץ, שינויים נשמרים רק אחרי commit. הדרך הנוחה: בלוק with con:, שעושה commit בסוף (ומבטל הכול אם הייתה שגיאה). cur.rowcount אומר כמה שורות הושפעו.UPDATE changes existing rows, DELETE removes them. Always with WHERE: without it, the change hits the whole table. In a file database, changes are saved only after a commit. The handy way: a with con: block, which commits at the end (and undoes everything if there was an error). cur.rowcount says how many rows were affected.
אנלוגיהAnalogy
commit הוא כמו ללחוץ "שלח" על מייל: אפשר לכתוב ולמחוק כמה שרוצים, אבל רק השליחה הופכת את זה לסופי.commit is like pressing "send" on an email: you can type and delete all you like, but only sending makes it final.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
GROUP BYGROUP BY
קיבוץ וחיבור טבלאותGrouping and joining
COUNT, SUM, AVG, MAXCOUNT, SUM, AVG, MAX
GROUP BY author: סיכום לכל קבוצהGROUP BY author: a summary per group
`JOIN`: חיבור לפי עמודה משותפת`JOIN`: combine by a shared column
SQL יודע לסכם: COUNT(*) סופר, AVG(pages) ממוצע. GROUP BY author נותן שורה אחת לכל סופר. וכשיש שתי טבלאות, ספרים וביקורות, JOIN מחבר אותן לפי עמודה משותפת (reviews.book_id = books.id), כך שכל ביקורת מופיעה עם כותרת הספר שלה.SQL can summarise: COUNT(*) counts, AVG(pages) averages. GROUP BY author gives one row per author. And with two tables, books and reviews, JOIN connects them by a shared column (reviews.book_id = books.id), so each review appears with its book's title.
אנלוגיהAnalogy
GROUP BY הוא כמו למיין כביסה לערימות לפי צבע ואז לספור כל ערימה. JOIN הוא כמו להצמיד לכל גרב את בן הזוג שלה לפי הדוגמה.GROUP BY is like sorting laundry into piles by colour then counting each pile. JOIN is like pairing each sock with its partner by pattern.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
JSON טוב לקצת נתוניםJSON is fine for a little data
למה מסד נתונים?Why a database?
מסד: חיפוש מהיר, עדכון של שורה אחתA database: fast search, update one row
למה מסד נתונים?Why a database?
SQLite מובנה בפייתון: קובץ אחדSQLite is built into Python: one file
למה מסד נתונים?Why a database?
CREATE TABLE עם עמודות וטיפוסיםCREATE TABLE with columns and types