Curriculum
Course: Grade XII Computer Science
Login
Text lesson

Ch-10 Python-SQL Connectivity

School Revise · Class 11 Computer Science · Chapter 10

Python-SQL Connectivity<div style=”max-width:860px;margin:0 auto;background:#ffffff;font-family:Arial,Helvetica,sans-serif;color:#22263a;overflow-x:hidden;”><div style=”background:#17458f;border-radius:14px;padding:30px 24px;text-align:center;”><p style=”margin:0 0 8px;color:#b9cdf0;font-size:12px;letter-spacing:2px;text-transform:uppercase;”>School Revise &#183; Class 11 Computer Science &#183; Chapter 10</p><h1 style=”margin:0 0 10px;color:#ffffff;font-size:30px;line-height:1.2;”>Python-SQL Connectivity</h1><p style=”margin:0 auto;max-width:640px;color:#d3ddf5;font-size:15px;line-height:1.7;”>Code 083, Class 12. Python can talk to a database directly. Here we connect, run queries, and read the results, all from Python.</p></div><table role=”presentation” style=”width:100%;border-collapse:collapse;margin:16px 0;”><tr><td style=”width:50%;padding:6px;”><table role=”presentation” style=”width:100%;background:#eef1fb;border-radius:10px;”><tr><td style=”padding:13px;text-align:center;”><p style=”margin:0;color:#203080;font-size:22px;font-weight:bold;”>connect</p><p style=”margin:2px 0 0;color:#5a5f75;font-size:12px;”>open a database</p></td></tr></table></td><td style=”width:50%;padding:6px;”><table role=”presentation” style=”width:100%;background:#fdeef5;border-radius:10px;”><tr><td style=”padding:13px;text-align:center;”><p style=”margin:0;color:#D80060;font-size:22px;font-weight:bold;”>cursor</p><p style=”margin:2px 0 0;color:#5a5f75;font-size:12px;”>run queries</p></td></tr></table></td></tr></table><h2 style=”color:#203080;font-size:21px;margin:26px 0 10px;border-left:5px solid #2b6cb0;padding-left:12px;word-break:normal;overflow-wrap:normal;”>What this chapter is about</h2><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0 0 12px;word-break:normal;overflow-wrap:normal;”>Python connects to a database so a program can store and read data. We open a <b>connection</b>, make a <b>cursor</b>, <b>execute</b> a query, and read the results.</p><h2 style=”color:#203080;font-size:21px;margin:26px 0 10px;border-left:5px solid #2b6cb0;padding-left:12px;word-break:normal;overflow-wrap:normal;”>1. Connect and cursor</h2><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0 0 12px;word-break:normal;overflow-wrap:normal;”><b>connect()</b> opens a database and <b>cursor()</b> makes an object that runs queries. In these examples we use SQLite, which comes with Python.</p><table role=”presentation” style=”width:100%;border-collapse:collapse;margin:0 0 12px;”><tr><td style=”background:#eef1fb;border-left:4px solid #2b6cb0;border-radius:9px;padding:12px 14px;”><p style=”margin:0;font-size:15px;line-height:1.7;color:#2a2f45;”>import sqlite3
con = sqlite3.connect(“:memory:”)
cur = con.cursor()
print(“connected”)
Output: connected</p></td></tr></table><h2 style=”color:#203080;font-size:21px;margin:26px 0 10px;border-left:5px solid #2b6cb0;padding-left:12px;word-break:normal;overflow-wrap:normal;”>2. Execute and commit</h2><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0 0 12px;word-break:normal;overflow-wrap:normal;”><b>execute()</b> runs a SQL command. After changing data (insert, update, delete), call <b>commit()</b> to save the changes.</p><h2 style=”color:#203080;font-size:21px;margin:26px 0 10px;border-left:5px solid #2b6cb0;padding-left:12px;word-break:normal;overflow-wrap:normal;”>3. Reading results</h2><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0 0 12px;word-break:normal;overflow-wrap:normal;”><b>fetchone()</b> returns one row, <b>fetchall()</b> returns all rows as a list, and <b>rowcount</b> tells how many rows were affected.</p><h2 style=”color:#203080;font-size:21px;margin:26px 0 10px;border-left:5px solid #2b6cb0;padding-left:12px;word-break:normal;overflow-wrap:normal;”>Practise with the interactive</h2><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0 0 12px;word-break:normal;overflow-wrap:normal;”>Explore the idea by tapping. The interactive opens right here in the lesson.</p>
<h2 style=”color:#203080;font-size:21px;margin:26px 0 10px;border-left:5px solid #2b6cb0;padding-left:12px;word-break:normal;overflow-wrap:normal;”>Try it in the code lab</h2><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0 0 12px;word-break:normal;overflow-wrap:normal;”>Write and run real Python right here in the lesson, then work through the practice problems with answers.</p>
Starting Python...
<h2 style=”color:#203080;font-size:21px;margin:26px 0 10px;border-left:5px solid #2b6cb0;padding-left:12px;word-break:normal;overflow-wrap:normal;”>Coding practice problems, with answers</h2><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0 0 12px;word-break:normal;overflow-wrap:normal;”>Type each one into the code lab above, then open the card to see the worked solution and its output.</p><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>Problem 1. Connect to a database.</summary><div style=”margin:8px 0 0;”><p style=”margin:0 0 4px;color:#0f6e56;font-size:12px;font-weight:bold;”>SOLUTION</p><div style=”background:#0f1b2a;color:#d7e6f5;font-family:Consolas,Menlo,monospace;font-size:13px;line-height:1.55;border-radius:8px;padding:10px 12px;white-space:pre;overflow-x:auto;”>import sqlite3
con = sqlite3.connect(“:memory:”)
print(“connected”)</div><p style=”margin:8px 0 4px;color:#5a5f75;font-size:12px;font-weight:bold;”>OUTPUT</p><div style=”background:#eef1fb;color:#22263a;font-family:Consolas,Menlo,monospace;font-size:13px;border-radius:8px;padding:8px 12px;white-space:pre;overflow-x:auto;”>connected</div></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>Problem 2. Make a cursor and create a table.</summary><div style=”margin:8px 0 0;”><p style=”margin:0 0 4px;color:#0f6e56;font-size:12px;font-weight:bold;”>SOLUTION</p><div style=”background:#0f1b2a;color:#d7e6f5;font-family:Consolas,Menlo,monospace;font-size:13px;line-height:1.55;border-radius:8px;padding:10px 12px;white-space:pre;overflow-x:auto;”>import sqlite3
con = sqlite3.connect(“:memory:”)
cur = con.cursor()
cur.execute(“CREATE TABLE student (rollno INT, name TEXT)”)
print(“table created”)</div><p style=”margin:8px 0 4px;color:#5a5f75;font-size:12px;font-weight:bold;”>OUTPUT</p><div style=”background:#eef1fb;color:#22263a;font-family:Consolas,Menlo,monospace;font-size:13px;border-radius:8px;padding:8px 12px;white-space:pre;overflow-x:auto;”>table created</div></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>Problem 3. Insert a row and commit.</summary><div style=”margin:8px 0 0;”><p style=”margin:0 0 4px;color:#0f6e56;font-size:12px;font-weight:bold;”>SOLUTION</p><div style=”background:#0f1b2a;color:#d7e6f5;font-family:Consolas,Menlo,monospace;font-size:13px;line-height:1.55;border-radius:8px;padding:10px 12px;white-space:pre;overflow-x:auto;”>import sqlite3
con = sqlite3.connect(“:memory:”)
cur = con.cursor()
cur.execute(“CREATE TABLE student (rollno INT, name TEXT)”)
cur.execute(“INSERT INTO student VALUES (1, ‘Ravi’)”)
con.commit()
print(“inserted”)</div><p style=”margin:8px 0 4px;color:#5a5f75;font-size:12px;font-weight:bold;”>OUTPUT</p><div style=”background:#eef1fb;color:#22263a;font-family:Consolas,Menlo,monospace;font-size:13px;border-radius:8px;padding:8px 12px;white-space:pre;overflow-x:auto;”>inserted</div></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>Problem 4. Fetch all rows.</summary><div style=”margin:8px 0 0;”><p style=”margin:0 0 4px;color:#0f6e56;font-size:12px;font-weight:bold;”>SOLUTION</p><div style=”background:#0f1b2a;color:#d7e6f5;font-family:Consolas,Menlo,monospace;font-size:13px;line-height:1.55;border-radius:8px;padding:10px 12px;white-space:pre;overflow-x:auto;”>import sqlite3
con = sqlite3.connect(“:memory:”)
cur = con.cursor()
cur.execute(“CREATE TABLE t (n INT)”)
cur.execute(“INSERT INTO t VALUES (1)”)
cur.execute(“INSERT INTO t VALUES (2)”)
cur.execute(“SELECT * FROM t”)
print(cur.fetchall())</div><p style=”margin:8px 0 4px;color:#5a5f75;font-size:12px;font-weight:bold;”>OUTPUT</p><div style=”background:#eef1fb;color:#22263a;font-family:Consolas,Menlo,monospace;font-size:13px;border-radius:8px;padding:8px 12px;white-space:pre;overflow-x:auto;”>[(1,), (2,)]</div></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>Problem 5. Fetch one row.</summary><div style=”margin:8px 0 0;”><p style=”margin:0 0 4px;color:#0f6e56;font-size:12px;font-weight:bold;”>SOLUTION</p><div style=”background:#0f1b2a;color:#d7e6f5;font-family:Consolas,Menlo,monospace;font-size:13px;line-height:1.55;border-radius:8px;padding:10px 12px;white-space:pre;overflow-x:auto;”>import sqlite3
con = sqlite3.connect(“:memory:”)
cur = con.cursor()
cur.execute(“CREATE TABLE t (n INT)”)
cur.execute(“INSERT INTO t VALUES (5)”)
cur.execute(“SELECT * FROM t”)
print(cur.fetchone())</div><p style=”margin:8px 0 4px;color:#5a5f75;font-size:12px;font-weight:bold;”>OUTPUT</p><div style=”background:#eef1fb;color:#22263a;font-family:Consolas,Menlo,monospace;font-size:13px;border-radius:8px;padding:8px 12px;white-space:pre;overflow-x:auto;”>(5,)</div></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>Problem 6. Count the rows in a table.</summary><div style=”margin:8px 0 0;”><p style=”margin:0 0 4px;color:#0f6e56;font-size:12px;font-weight:bold;”>SOLUTION</p><div style=”background:#0f1b2a;color:#d7e6f5;font-family:Consolas,Menlo,monospace;font-size:13px;line-height:1.55;border-radius:8px;padding:10px 12px;white-space:pre;overflow-x:auto;”>import sqlite3
con = sqlite3.connect(“:memory:”)
cur = con.cursor()
cur.execute(“CREATE TABLE t (n INT)”)
cur.executemany(“INSERT INTO t VALUES (?)”, [(1,), (2,), (3,)])
cur.execute(“SELECT COUNT(*) FROM t”)
print(cur.fetchone()[0])</div><p style=”margin:8px 0 4px;color:#5a5f75;font-size:12px;font-weight:bold;”>OUTPUT</p><div style=”background:#eef1fb;color:#22263a;font-family:Consolas,Menlo,monospace;font-size:13px;border-radius:8px;padding:8px 12px;white-space:pre;overflow-x:auto;”>3</div></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>Problem 7. Select rows with a WHERE clause.</summary><div style=”margin:8px 0 0;”><p style=”margin:0 0 4px;color:#0f6e56;font-size:12px;font-weight:bold;”>SOLUTION</p><div style=”background:#0f1b2a;color:#d7e6f5;font-family:Consolas,Menlo,monospace;font-size:13px;line-height:1.55;border-radius:8px;padding:10px 12px;white-space:pre;overflow-x:auto;”>import sqlite3
con = sqlite3.connect(“:memory:”)
cur = con.cursor()
cur.execute(“CREATE TABLE s (name TEXT, marks INT)”)
cur.execute(“INSERT INTO s VALUES (‘Ravi’, 82)”)
cur.execute(“INSERT INTO s VALUES (‘Sana’, 91)”)
cur.execute(“SELECT name FROM s WHERE marks &gt; 85”)
print(cur.fetchall())</div><p style=”margin:8px 0 4px;color:#5a5f75;font-size:12px;font-weight:bold;”>OUTPUT</p><div style=”background:#eef1fb;color:#22263a;font-family:Consolas,Menlo,monospace;font-size:13px;border-radius:8px;padding:8px 12px;white-space:pre;overflow-x:auto;”>[(‘Sana’,)]</div></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>Problem 8. Update a value from Python.</summary><div style=”margin:8px 0 0;”><p style=”margin:0 0 4px;color:#0f6e56;font-size:12px;font-weight:bold;”>SOLUTION</p><div style=”background:#0f1b2a;color:#d7e6f5;font-family:Consolas,Menlo,monospace;font-size:13px;line-height:1.55;border-radius:8px;padding:10px 12px;white-space:pre;overflow-x:auto;”>import sqlite3
con = sqlite3.connect(“:memory:”)
cur = con.cursor()
cur.execute(“CREATE TABLE s (rollno INT, marks INT)”)
cur.execute(“INSERT INTO s VALUES (1, 70)”)
cur.execute(“UPDATE s SET marks = 95 WHERE rollno = 1”)
cur.execute(“SELECT marks FROM s”)
print(cur.fetchone()[0])</div><p style=”margin:8px 0 4px;color:#5a5f75;font-size:12px;font-weight:bold;”>OUTPUT</p><div style=”background:#eef1fb;color:#22263a;font-family:Consolas,Menlo,monospace;font-size:13px;border-radius:8px;padding:8px 12px;white-space:pre;overflow-x:auto;”>95</div></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>Problem 9. Read the average marks.</summary><div style=”margin:8px 0 0;”><p style=”margin:0 0 4px;color:#0f6e56;font-size:12px;font-weight:bold;”>SOLUTION</p><div style=”background:#0f1b2a;color:#d7e6f5;font-family:Consolas,Menlo,monospace;font-size:13px;line-height:1.55;border-radius:8px;padding:10px 12px;white-space:pre;overflow-x:auto;”>import sqlite3
con = sqlite3.connect(“:memory:”)
cur = con.cursor()
cur.execute(“CREATE TABLE s (marks INT)”)
cur.executemany(“INSERT INTO s VALUES (?)”, [(80,), (90,)])
cur.execute(“SELECT AVG(marks) FROM s”)
print(cur.fetchone()[0])</div><p style=”margin:8px 0 4px;color:#5a5f75;font-size:12px;font-weight:bold;”>OUTPUT</p><div style=”background:#eef1fb;color:#22263a;font-family:Consolas,Menlo,monospace;font-size:13px;border-radius:8px;padding:8px 12px;white-space:pre;overflow-x:auto;”>85.0</div></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>Problem 10. Close the connection.</summary><div style=”margin:8px 0 0;”><p style=”margin:0 0 4px;color:#0f6e56;font-size:12px;font-weight:bold;”>SOLUTION</p><div style=”background:#0f1b2a;color:#d7e6f5;font-family:Consolas,Menlo,monospace;font-size:13px;line-height:1.55;border-radius:8px;padding:10px 12px;white-space:pre;overflow-x:auto;”>import sqlite3
con = sqlite3.connect(“:memory:”)
con.close()
print(“closed”)</div><p style=”margin:8px 0 4px;color:#5a5f75;font-size:12px;font-weight:bold;”>OUTPUT</p><div style=”background:#eef1fb;color:#22263a;font-family:Consolas,Menlo,monospace;font-size:13px;border-radius:8px;padding:8px 12px;white-space:pre;overflow-x:auto;”>closed</div></div></details><h2 style=”color:#203080;font-size:21px;margin:26px 0 10px;border-left:5px solid #2b6cb0;padding-left:12px;word-break:normal;overflow-wrap:normal;”>Practice set A, multiple choice</h2><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>1. Which function opens a database?</summary><div style=”margin:8px 0 0;”><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0;”>connect().</p></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>2. Which object runs queries?</summary><div style=”margin:8px 0 0;”><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0;”>The cursor.</p></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>3. Which method runs a SQL command?</summary><div style=”margin:8px 0 0;”><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0;”>execute().</p></div></details><details style=”background:#fff;border:1px solid #e6e8f2;border-radius:9px;padding:8px 12px;margin:0 0 8px;”><summary style=”cursor:pointer;color:#203080;font-size:14.5px;font-weight:500;”>4. Which method returns all rows?</summary><div style=”margin:8px 0 0;”><p style=”font-size:15px;line-height:1.75;color:#33384a;margin:0;”>fetchall().</p></div></details><h2 style=”color:#203080;font-size:21px;margin:26px 0 10px;border-left:5px solid #2b6cb0;padding-left:12px;word-break:normal;overflow-wrap:normal;”>Quick summary</h2><table role=”presentation” style=”width:100%;border-collapse:collapse;margin:0 0 8px;”><tr><td style=”background:#eef1fb;border-radius:10px;padding:14px 16px;”><p style=”margin:0;font-size:14.5px;line-height:1.8;color:#2a2f45;”>Python connects to a database so a program can store and read data. connect() opens the database, cursor() makes an object to run queries, execute() runs a SQL command, and commit() saves changes. fetchone() returns one row, fetchall() returns all rows, and rowcount gives the number of rows affected.</p></td></tr></table><table role=”presentation” style=”border-collapse:collapse;margin:16px 0 0;”><tr><td style=”background:#17458f;border-radius:9px;padding:10px 18px;”><a href=”https://schoolrevise.com/virtual-lab/” target=”_blank” rel=”noopener” style=”color:#fff;text-decoration:none;font-size:14px;font-weight:500;”>Open the Virtual Lab</a></td></tr></table><div style=”background:#f4f5fb;border-radius:12px;padding:16px 18px;margin:22px 0 0;”><p style=”margin:0;color:#5a5f75;font-size:13px;line-height:1.8;”>These free Class 12 Computer Science notes explain connecting Python with SQL, connect and cursor, execute and commit, and fetchone fetchall and rowcount with worked examples and practice, for CBSE students across India and the Gulf including the UAE, Saudi Arabia, Qatar, Oman, Kuwait and Bahrain.</p></div><div style=”border-top:1px solid #e6e8f2;margin:16px 0 0;padding:12px 4px 2px;”><p style=”margin:0;color:#9aa0b4;font-size:12px;line-height:1.7;”>&#169; 2026 School Revise. All rights reserved. Original content aligned to the CBSE and NCERT Class 12 Computer Science syllabus. Unauthorised copying is not permitted.</p></div></div>

Code 083, Class 12. Python can talk to a database directly. Here we connect, run queries, and read the results, all from Python.

connect

open a database

cursor

run queries

What this chapter is about

Python connects to a database so a program can store and read data. We open a connection, make a cursor, execute a query, and read the results.

1. Connect and cursor

connect() opens a database and cursor() makes an object that runs queries. In these examples we use SQLite, which comes with Python.

import sqlite3 con = sqlite3.connect(“:memory:”) cur = con.cursor() print(“connected”) Output: connected

2. Execute and commit

execute() runs a SQL command. After changing data (insert, update, delete), call commit() to save the changes.

3. Reading results

fetchone() returns one row, fetchall() returns all rows as a list, and rowcount tells how many rows were affected.

Practise with the interactive

Explore the idea by tapping. The interactive opens right here in the lesson.

Try it in the code lab

Write and run real Python right here in the lesson, then work through the practice problems with answers.

Starting Python...

Coding practice problems, with answers

Type each one into the code lab above, then open the card to see the worked solution and its output.

Problem 1. Connect to a database.

SOLUTION

import sqlite3 con = sqlite3.connect(“:memory:”) print(“connected”)

OUTPUT

connected
Problem 2. Make a cursor and create a table.

SOLUTION

import sqlite3 con = sqlite3.connect(“:memory:”) cur = con.cursor() cur.execute(“CREATE TABLE student (rollno INT, name TEXT)”) print(“table created”)

OUTPUT

table created
Problem 3. Insert a row and commit.

SOLUTION

import sqlite3 con = sqlite3.connect(“:memory:”) cur = con.cursor() cur.execute(“CREATE TABLE student (rollno INT, name TEXT)”) cur.execute(“INSERT INTO student VALUES (1, ‘Ravi’)”) con.commit() print(“inserted”)

OUTPUT

inserted
Problem 4. Fetch all rows.

SOLUTION

import sqlite3 con = sqlite3.connect(“:memory:”) cur = con.cursor() cur.execute(“CREATE TABLE t (n INT)”) cur.execute(“INSERT INTO t VALUES (1)”) cur.execute(“INSERT INTO t VALUES (2)”) cur.execute(“SELECT * FROM t”) print(cur.fetchall())

OUTPUT

[(1,), (2,)]
Problem 5. Fetch one row.

SOLUTION

import sqlite3 con = sqlite3.connect(“:memory:”) cur = con.cursor() cur.execute(“CREATE TABLE t (n INT)”) cur.execute(“INSERT INTO t VALUES (5)”) cur.execute(“SELECT * FROM t”) print(cur.fetchone())

OUTPUT

(5,)
Problem 6. Count the rows in a table.

SOLUTION

import sqlite3 con = sqlite3.connect(“:memory:”) cur = con.cursor() cur.execute(“CREATE TABLE t (n INT)”) cur.executemany(“INSERT INTO t VALUES (?)”, [(1,), (2,), (3,)]) cur.execute(“SELECT COUNT(*) FROM t”) print(cur.fetchone()[0])

OUTPUT

3
Problem 7. Select rows with a WHERE clause.

SOLUTION

import sqlite3 con = sqlite3.connect(“:memory:”) cur = con.cursor() cur.execute(“CREATE TABLE s (name TEXT, marks INT)”) cur.execute(“INSERT INTO s VALUES (‘Ravi’, 82)”) cur.execute(“INSERT INTO s VALUES (‘Sana’, 91)”) cur.execute(“SELECT name FROM s WHERE marks > 85”) print(cur.fetchall())

OUTPUT

[(‘Sana’,)]
Problem 8. Update a value from Python.

SOLUTION

import sqlite3 con = sqlite3.connect(“:memory:”) cur = con.cursor() cur.execute(“CREATE TABLE s (rollno INT, marks INT)”) cur.execute(“INSERT INTO s VALUES (1, 70)”) cur.execute(“UPDATE s SET marks = 95 WHERE rollno = 1”) cur.execute(“SELECT marks FROM s”) print(cur.fetchone()[0])

OUTPUT

95
Problem 9. Read the average marks.

SOLUTION

import sqlite3 con = sqlite3.connect(“:memory:”) cur = con.cursor() cur.execute(“CREATE TABLE s (marks INT)”) cur.executemany(“INSERT INTO s VALUES (?)”, [(80,), (90,)]) cur.execute(“SELECT AVG(marks) FROM s”) print(cur.fetchone()[0])

OUTPUT

85.0
Problem 10. Close the connection.

SOLUTION

import sqlite3 con = sqlite3.connect(“:memory:”) con.close() print(“closed”)

OUTPUT

closed

Practice set A, multiple choice

1. Which function opens a database?

connect().

2. Which object runs queries?

The cursor.

3. Which method runs a SQL command?

execute().

4. Which method returns all rows?

fetchall().

Quick summary

Python connects to a database so a program can store and read data. connect() opens the database, cursor() makes an object to run queries, execute() runs a SQL command, and commit() saves changes. fetchone() returns one row, fetchall() returns all rows, and rowcount gives the number of rows affected.

Open the Virtual Lab

These free Class 12 Computer Science notes explain connecting Python with SQL, connect and cursor, execute and commit, and fetchone fetchall and rowcount with worked examples and practice, for CBSE students across India and the Gulf including the UAE, Saudi Arabia, Qatar, Oman, Kuwait and Bahrain.

© 2026 School Revise. All rights reserved. Original content aligned to the CBSE and NCERT Class 12 Computer Science syllabus. Unauthorised copying is not permitted.

Layer 1
Login Categories