© 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
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() සුරකියි; ආරක්ෂාවට ?.
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. පියවර
- 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)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 ඉතිරි පේළි දෙක දෙයි.
Key Wordsප්රධාන පද
Sample Q&Aආදර්ශ ප්රශ්න සහ පිළිතුරු
1Four operations from Python?මෙහෙයුම් හතර?
2Role of a cursor?cursor?
3Why commit()?commit() ඇයි?
4Kamala’s mark after UPDATE?Kamala ලකුණ?
5How many rows does SELECT return?පේළි කීයද?
6Why use ? placeholders?? ඇයි?
MCQබහුවරණ ප්රශ්න
Choose the correct answer for each question.සෑම ප්රශ්නයකටම නිවැරදි පිළිතුර තෝරන්න.
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 පිළිතුරු
- A – sqlite3.sqlite3.
- B – Cursor.Cursor.
- A – Commit.Commit.
- D – UPDATE.UPDATE.
- A – WHERE ID = S003.S003.
- E – ORDER BY Name.ORDER BY Name.
- D – SELECT.SELECT.
- E – Injection.Injection.
- C – Database connection – Link between a program and a database.දත්ත සමුදා සම්බන්ධතාව – වැඩසටහනක් සහ දත්ත සමුදායක් අතර සබැඳිය.
- B – sqlite3 – Built-in Python module for SQLite databases.sqlite3 – SQLite සඳහා Python module.
- C – connect() – Function that opens a database connection.connect() – සම්බන්ධතාවක් විවෘත කරන ශ්රිතය.
- E – Cursor – Object that executes SQL and holds results.Cursor – SQL ධාවනය කර ප්රතිඵල රඳවන වස්තුව.
- B – execute() – Method that runs one SQL statement.execute() – එක් SQL ප්රකාශයක් ධාවනය කරන method.
- B – commit() – Method that saves changes permanently.commit() – වෙනස්කම් ස්ථිරව සුරකින method.
- D – INSERT – SQL command that adds a record.INSERT – වාර්තාවක් එක් කරන SQL විධානය.
- A – UPDATE – SQL command that modifies records.UPDATE – වාර්තා වෙනස් කරන SQL විධානය.
- C – DELETE – SQL command that removes records.DELETE – වාර්තා ඉවත් කරන SQL විධානය.
- E – SELECT – SQL command that retrieves records.SELECT – වාර්තා ලබා ගන්නා SQL විධානය.
- C – Placeholder – The ? mark that safely takes a value in SQL.Placeholder – SQL හි අගයක් ආරක්ෂිතව ගන්නා ? ලකුණ.
- 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ආදර්ශ පිළිතුර පෙන්වන්න
2Write code to insert a student and display all students.INSERT සහ SELECT. (4 marksලකුණු 4)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
3Explain why the program prints two rows only.පේළි දෙක. (4 marksලකුණු 4)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
4Explain the purpose of commit() and close().commit සහ close. (2 marksලකුණු 2)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
5What is SQL injection and how is it prevented?SQL injection. (2 marksලකුණු 2)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
🎮 Game🎮 ක්රීඩා
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බාගත කිරීම්
Resource Book Lesson Note Download Questions Syllabus Sinhala LMS Simulator Tool
