School Revise · Class 11 Computer Science · Chapter 10
Code 083, Class 12. Python can talk to a database directly. Here we connect, run queries, and read the results, all from Python.
|
|
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.
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 |
execute() runs a SQL command. After changing data (insert, update, delete), call commit() to save the changes.
fetchone() returns one row, fetchall() returns all rows as a list, and rowcount tells how many rows were affected.
Explore the idea by tapping. The interactive opens right here in the lesson.
Write and run real Python right here in the lesson, then work through the practice problems with answers.
Type each one into the code lab above, then open the card to see the worked solution and its output.
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
SOLUTION
OUTPUT
connect().
The cursor.
execute().
fetchall().
|
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.