Python and Databases: Connect, Add, Modify, Delete, Retrieve – Grade 13 A/L ICT Notes 9.12

© 2026 schoolict.net (Ruwan Mangala Suraweera)

9.12 Python and Databases: Connect, Add, Modify, Delete, RetrievePython සහ දත්ත සමුදා: සම්බන්ධ, එකතු, වෙනස්, මැකීම, ලබා ගැනීම

Grade 13 A/L ICT · Lesson 913 ශ්‍රේණිය A/L ICT · පාඩම 9

Prints all tabs in the language you are reading. Use “Save as PDF” in the print window to keep a copy.ඔබ කියවන භාෂාවෙන් සියලු tabs මුද්‍රණය වේ. පිටපතක් තියාගන්න print window එකේ “Save as PDF” තෝරන්න.
🎬Video coming soon in this language. Switch language to watch the other video.මේ භාෂාවෙන් වීඩියෝව ඉක්මනින් එනවා. අනෙක් වීඩියෝව බලන්න භාෂාව මාරු කරන්න.

In this videoමේ වීඩියෝවේ

  • connect() opens the database.connect() විවෘත කරයි.
  • A cursor executes SQL.cursor SQL ධාවනය කරයි.
  • INSERT, UPDATE, DELETE change data.INSERT, UPDATE, DELETE වෙනස් කරයි.
  • SELECT retrieves rows.SELECT පේළි ලබා දෙයි.
  • commit() saves; use ? for safety.commit() සුරකියි; ආරක්ෂාවට ?.
Database connectionsqlite3connect()Cursorexecute()commit()INSERT

Notesසටහන්

Syllabus 9.12. A program can connect to a database and run SQL to add, modify, delete and retrieve records. Python’s built-in sqlite3 module needs no server: connect() opens the database, a cursor runs SQL with execute(), commit() saves changes and close() disconnects.විෂය නිර්දේශය 9.12. වැඩසටහනකට දත්ත සමුදායකට සම්බන්ධ වී SQL මගින් වාර්තා එකතු, වෙනස්, මැකීම සහ ලබා ගැනීම කළ හැක. Python හි sqlite3 module එකට සේවාදායකයක් අවශ්‍ය නැත: connect() විවෘත කරයි, cursor execute() මගින් SQL ධාවනය කරයි, commit() සුරකියි, close() විසන්ධි කරයි.

1. Steps1. පියවර

Python program and a databasePythonprogramsqlite3connect()cursorexecute(SQL)DatabasetablesINSERT, UPDATE, DELETE need commit(); SELECT rows come back as tuples.
Python program talking to a databaseදත්ත සමුදාය සමඟ Python වැඩසටහන
  • Connect: con = sqlite3.connect(“school.db”).සම්බන්ධ: con = sqlite3.connect(“school.db”).
  • Cursor: cur = con.cursor() runs SQL statements.Cursor: cur = con.cursor() SQL ධාවනය කරයි.
  • Add / modify / delete: INSERT, UPDATE, DELETE then con.commit().එකතු / වෙනස් / මැකීම: INSERT, UPDATE, DELETE පසු con.commit().
  • Retrieve: SELECT and loop over the rows; then con.close().ලබා ගැනීම: SELECT, පේළි හරහා loop; පසු con.close().

🐍 Python with an SQLite databaseSQLite දත්ත සමුදාය සමඟ Python

import sqlite3
con = sqlite3.connect(":memory:")       # connect
cur = con.cursor()
cur.execute("CREATE TABLE Student (ID TEXT PRIMARY KEY, Name TEXT, Mark INTEGER)")
rows = [("S001", "Nimal", 78), ("S002", "Kamala", 45), ("S003", "Sunil", 88)]
cur.executemany("INSERT INTO Student VALUES (?, ?, ?)", rows)       # add
cur.execute("UPDATE Student SET Mark = 50 WHERE ID = ?", ("S002",))  # modify
cur.execute("DELETE FROM Student WHERE ID = ?", ("S003",))           # delete
con.commit()
for row in cur.execute("SELECT Name, Mark FROM Student ORDER BY Name"):  # retrieve
    print(row)
con.close()

▶ Outputප්‍රතිදානය

('Kamala', 50)
('Nimal', 78)
🧮 Worked exampleවිසඳූ උදාහරණය Why are only Kamala 50 and Nimal 78 printed?Kamala 50 සහ Nimal 78 පමණක් ඇයි?
Three rows are inserted. UPDATE changes S002’s mark from 45 to 50; DELETE removes S003 (Sunil). SELECT … ORDER BY Name returns the two rows left in name order.පේළි තුනක් ඇතුළු වේ. UPDATE S002 ලකුණ 45 සිට 50; DELETE S003 ඉවත් කරයි. ORDER BY Name ඉතිරි පේළි දෙක දෙයි.
💡 Use ? placeholders, never string joining, to put values into SQL – this stops SQL injection.SQL වෙත අගයන් දැමීමට ? placeholders භාවිත කරන්න – SQL injection වළක්වයි.
🚀 Beyond the syllabus (2026 update)🚀 විෂය නිර්දේශයෙන් ඔබ්බට (2026 යාවත්කාලීනය) Not required for the exam – read for interest.විභාගයට අවශ්‍ය නැත – දැනුම සඳහා කියවන්න. Web apps in 2026 often use an ORM such as SQLAlchemy or Django, which writes this SQL for you, but the same connect–execute–commit idea lies underneath.2026 web යෙදුම් SQLAlchemy, Django වැනි ORM භාවිත කරයි; යටින් connect–execute–commit එකමයි.

Key Wordsප්‍රධාන පද

Database connectionදත්ත සමුදා සම්බන්ධතාව
Link between a program and a database.වැඩසටහනක් සහ දත්ත සමුදායක් අතර සබැඳිය.
sqlite3sqlite3
Built-in Python module for SQLite databases.SQLite සඳහා Python module.
connect()connect()
Function that opens a database connection.සම්බන්ධතාවක් විවෘත කරන ශ්‍රිතය.
CursorCursor
Object that executes SQL and holds results.SQL ධාවනය කර ප්‍රතිඵල රඳවන වස්තුව.
execute()execute()
Method that runs one SQL statement.එක් SQL ප්‍රකාශයක් ධාවනය කරන method.
commit()commit()
Method that saves changes permanently.වෙනස්කම් ස්ථිරව සුරකින method.
INSERTINSERT
SQL command that adds a record.වාර්තාවක් එක් කරන SQL විධානය.
UPDATEUPDATE
SQL command that modifies records.වාර්තා වෙනස් කරන SQL විධානය.
DELETEDELETE
SQL command that removes records.වාර්තා ඉවත් කරන SQL විධානය.
SELECTSELECT
SQL command that retrieves records.වාර්තා ලබා ගන්නා SQL විධානය.
PlaceholderPlaceholder
The ? mark that safely takes a value in SQL.SQL හි අගයක් ආරක්ෂිතව ගන්නා ? ලකුණ.
SQL injectionSQL injection
Attack that inserts harmful SQL through input.ආදානය හරහා හානිකර SQL ඇතුළු කරන ප්‍රහාරය.

Sample Q&Aආදර්ශ ප්‍රශ්න සහ පිළිතුරු

1Four operations from Python?මෙහෙයුම් හතර?

Answerපිළිතුර: Add, modify, delete, retrieve.එකතු, වෙනස්, මැකීම, ලබා ගැනීම.

2Role of a cursor?cursor?

Answerපිළිතුර: It executes SQL and returns rows.SQL ධාවනය කරයි.

3Why commit()?commit() ඇයි?

Answerපිළිතුර: Changes are not saved until commit.සුරැකීමට.

4Kamala’s mark after UPDATE?Kamala ලකුණ?

Answerපිළිතුර: 50.50.

5How many rows does SELECT return?පේළි කීයද?

Answerපිළිතුර: 2.2.

6Why use ? placeholders?? ඇයි?

Answerපිළිතුර: Safety against SQL injection.SQL injection වැළැක්වීම.

MCQබහුවරණ ප්‍රශ්න

Choose the correct answer for each question.සෑම ප්‍රශ්නයකටම නිවැරදි පිළිතුර තෝරන්න.

Scoreලකුණු: 0 / 20

1Python’s built-in module for SQLite ismodule

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

sqlite3.sqlite3.

2Which object runs SQL statements?SQL ධාවනය

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

Cursor.Cursor.

3Changes are saved permanently byසුරැකීම

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

Commit.Commit.

4After the program, Kamala’s mark isKamala

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

UPDATE.UPDATE.

5Which record is removed by DELETE?DELETE

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

WHERE ID = S003.S003.

6The first row printed isපළමු පේළිය

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

ORDER BY Name.ORDER BY Name.

7Retrieving records usesලබා ගැනීම

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

SELECT.SELECT.

8? placeholders mainly protect against?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Injection.Injection.

9Which term means: “Link between a program and a database”?“වැඩසටහනක් සහ දත්ත සමුදායක් අතර සබැඳිය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Database connection – Link between a program and a database.දත්ත සමුදා සම්බන්ධතාව – වැඩසටහනක් සහ දත්ත සමුදායක් අතර සබැඳිය.

10Which term means: “Built-in Python module for SQLite databases”?“SQLite සඳහා Python module” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

sqlite3 – Built-in Python module for SQLite databases.sqlite3 – SQLite සඳහා Python module.

11Which term means: “Function that opens a database connection”?“සම්බන්ධතාවක් විවෘත කරන ශ්‍රිතය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

connect() – Function that opens a database connection.connect() – සම්බන්ධතාවක් විවෘත කරන ශ්‍රිතය.

12Which term means: “Object that executes SQL and holds results”?“SQL ධාවනය කර ප්‍රතිඵල රඳවන වස්තුව” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Cursor – Object that executes SQL and holds results.Cursor – SQL ධාවනය කර ප්‍රතිඵල රඳවන වස්තුව.

13Which term means: “Method that runs one SQL statement”?“එක් SQL ප්‍රකාශයක් ධාවනය කරන method” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

execute() – Method that runs one SQL statement.execute() – එක් SQL ප්‍රකාශයක් ධාවනය කරන method.

14Which term means: “Method that saves changes permanently”?“වෙනස්කම් ස්ථිරව සුරකින method” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

commit() – Method that saves changes permanently.commit() – වෙනස්කම් ස්ථිරව සුරකින method.

15Which term means: “SQL command that adds a record”?“වාර්තාවක් එක් කරන SQL විධානය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

INSERT – SQL command that adds a record.INSERT – වාර්තාවක් එක් කරන SQL විධානය.

16Which term means: “SQL command that modifies records”?“වාර්තා වෙනස් කරන SQL විධානය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

UPDATE – SQL command that modifies records.UPDATE – වාර්තා වෙනස් කරන SQL විධානය.

17Which term means: “SQL command that removes records”?“වාර්තා ඉවත් කරන SQL විධානය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

DELETE – SQL command that removes records.DELETE – වාර්තා ඉවත් කරන SQL විධානය.

18Which term means: “SQL command that retrieves records”?“වාර්තා ලබා ගන්නා SQL විධානය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

SELECT – SQL command that retrieves records.SELECT – වාර්තා ලබා ගන්නා SQL විධානය.

19Which term means: “The ? mark that safely takes a value in SQL”?“SQL හි අගයක් ආරක්ෂිතව ගන්නා ? ලකුණ” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Placeholder – The ? mark that safely takes a value in SQL.Placeholder – SQL හි අගයක් ආරක්ෂිතව ගන්නා ? ලකුණ.

20Which term means: “Attack that inserts harmful SQL through input”?“ආදානය හරහා හානිකර SQL ඇතුළු කරන ප්‍රහාරය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

SQL injection – Attack that inserts harmful SQL through input.SQL injection – ආදානය හරහා හානිකර SQL ඇතුළු කරන ප්‍රහාරය.

MCQ answer keyMCQ පිළිතුරු

  1. A – sqlite3.sqlite3.
  2. B – Cursor.Cursor.
  3. A – Commit.Commit.
  4. D – UPDATE.UPDATE.
  5. A – WHERE ID = S003.S003.
  6. E – ORDER BY Name.ORDER BY Name.
  7. D – SELECT.SELECT.
  8. E – Injection.Injection.
  9. C – Database connection – Link between a program and a database.දත්ත සමුදා සම්බන්ධතාව – වැඩසටහනක් සහ දත්ත සමුදායක් අතර සබැඳිය.
  10. B – sqlite3 – Built-in Python module for SQLite databases.sqlite3 – SQLite සඳහා Python module.
  11. C – connect() – Function that opens a database connection.connect() – සම්බන්ධතාවක් විවෘත කරන ශ්‍රිතය.
  12. E – Cursor – Object that executes SQL and holds results.Cursor – SQL ධාවනය කර ප්‍රතිඵල රඳවන වස්තුව.
  13. B – execute() – Method that runs one SQL statement.execute() – එක් SQL ප්‍රකාශයක් ධාවනය කරන method.
  14. B – commit() – Method that saves changes permanently.commit() – වෙනස්කම් ස්ථිරව සුරකින method.
  15. D – INSERT – SQL command that adds a record.INSERT – වාර්තාවක් එක් කරන SQL විධානය.
  16. A – UPDATE – SQL command that modifies records.UPDATE – වාර්තා වෙනස් කරන SQL විධානය.
  17. C – DELETE – SQL command that removes records.DELETE – වාර්තා ඉවත් කරන SQL විධානය.
  18. E – SELECT – SQL command that retrieves records.SELECT – වාර්තා ලබා ගන්නා SQL විධානය.
  19. C – Placeholder – The ? mark that safely takes a value in SQL.Placeholder – SQL හි අගයක් ආරක්ෂිතව ගන්නා ? ලකුණ.
  20. D – SQL injection – Attack that inserts harmful SQL through input.SQL injection – ආදානය හරහා හානිකර SQL ඇතුළු කරන ප්‍රහාරය.

Structured Questionsව්‍යුහගත ප්‍රශ්න

1List the steps to use a database from Python.පියවර. (5 marksලකුණු 5)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
connect; cursor; execute; commit; close.connect; cursor; execute; commit; close.

2Write code to insert a student and display all students.INSERT සහ SELECT. (4 marksලකුණු 4)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
execute INSERT with ?; commit; for row in SELECT; print.INSERT; commit; SELECT.

3Explain why the program prints two rows only.පේළි දෙක. (4 marksලකුණු 4)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
Three inserted; one deleted; one updated; order.3; DELETE; UPDATE.

4Explain the purpose of commit() and close().commit සහ close. (2 marksලකුණු 2)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
commit saves; close frees the connection.සුරැකීම; නිදහස් කිරීම.

5What is SQL injection and how is it prevented?SQL injection. (2 marksලකුණු 2)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
Harmful input in SQL; use placeholders.හානිකර ආදානය; placeholders.

🎮 Game🎮 ක්‍රීඩා

Pick a game. Each one uses words from this lesson.ක්‍රීඩාවක් තෝරන්න. හැම එකක්ම මේ පාඩමේ වචන පාවිච්චි කරනවා.

Summary & Downloadsසාරාංශය සහ බාගත කිරීම්

Key pointsප්‍රධාන කරුණු

  • connect() opens the database.connect() විවෘත කරයි.
  • A cursor executes SQL.cursor SQL ධාවනය කරයි.
  • INSERT, UPDATE, DELETE change data.INSERT, UPDATE, DELETE වෙනස් කරයි.
  • SELECT retrieves rows.SELECT පේළි ලබා දෙයි.
  • commit() saves; use ? for safety.commit() සුරකියි; ආරක්ෂාවට ?.

Downloadsබාගත කිරීම්

Full Lesson 9 PDFසම්පූර්ණ පාඩම 9 PDF: English · සිංහල

Next noteඊළඟ සටහන 9.13 →

© 2026 schoolict.net (Ruwan Mangala Suraweera). All rights reserved.සියලු හිමිකම් ඇවිරිණි.

Resource Book Lesson Note Download Questions Syllabus Sinhala LMS Simulator Tool