TN Online TestSamacheer Kalvi practice

Data manipulation through SQL - Study Notes

Share this chapter: Telegram

Chapter Summary

This chapter connects Python to a database using SQLite, a small database library that comes with Python as the sqlite3 module. A Python script can create a database and tables, insert, update and delete records, and run SELECT queries with clauses such as DISTINCT, WHERE, GROUP BY, ORDER BY and HAVING. The chapter also uses aggregate functions and shows how to write the result of a query into a CSV file.

Learning Objectives

Key Concepts and Definitions

SQLite

A lightweight relational database that runs inside the application itself. It is serverless, so no separate database server has to be installed or started.

Connection Object

The link between the Python program and the SQLite database file. It is created with the connect() method.

Cursor Object

A control structure used to traverse and fetch the records of the database. SQL statements are run through the cursor with execute(), and the rows they return are read from it.

Placeholder

A symbol, such as a question mark ? or a named parameter, written in an SQL statement in place of a value. The actual value is supplied when the statement runs, which is safer than joining strings together.

Commit

Saves all changes made in the current transaction to the database file for good.

Rollback

Cancels the changes made since the last commit and returns the database to that state.

Worked Methods

Connecting to a Database and Creating a Table

Every program in this chapter follows the same pattern: connect, get a cursor, run the SQL, commit, close.

First, import the module with import sqlite3. Then call connect() with a file name; if that database file does not exist, it is created. Next, get a cursor with the cursor() method. Put the CREATE TABLE statement in a string and pass it to execute(). Finally, call commit and close the connection.

Inserting and Retrieving Records

To add records, run an INSERT INTO statement through execute(). To read records, run a SELECT query and then call fetchall() to get every row, or fetchone() to get one row at a time, as Python objects.

Common Exam Traps

Exam Tips

  1. Write longer SQL statements in triple quotes. The query can then run over several lines, and single or double quotes inside it cause no trouble.
  2. Call close() when you have finished, so the connection and its resources are released.
  3. ORDER BY only sorts the rows in the output. The order of rows stored in the table does not change.
  4. sqlite_master is the master table that stores the structure of every table in the database. Querying it is how you list the table names.
Solved MCQs → Practice test →

More for this chapter

Book Back Questions10 textbook MCQs · solved Additional MCQs15 extra MCQs · solved Practice TestInteractive · instant score Book Back TestTest yourself on the textbook set Additional MCQ TestTest yourself on the extra set Formula SheetAll key formulas

More chapters in Computer Science

View all
1 Function 2 Data Abstraction 3 Scoping 4 Algorithmic Strategies 5 Python Variables and Operators 6 Control Structures 7 Python Functions 8 Strings and String Manipulation 9 Lists, Tuples, Sets and Dictionary 10 Python Classes and Objects 11 Database Concepts 12 Structured Query Language (SQL) 13 Python and CSV Files 14 Importing C++ Programs in Python 16 Data Visualization using Pyplot: Line Chart, Pie Chart and Bar Chart