liveBook · Manning

Cursor

As explained in A Pythonic Adventure: From Python basics to a working web app

A database cursor is an object that executes database statements and retrieves their results.

A cursor is the database object that points into a connection and executes SQL commands. In a SQLite-backed order app, create one with `cur = con.cursor()` before asking the database to create tables, save orders, or retrieve them. section 12.0

Using a cursor for orders

After connecting to `orders.db`, call `cur.execute()` with `CREATE TABLE IF NOT EXISTS orders(...)` to prepare the table. Saving an order uses another `cur.execute()` call with `INSERT`, followed by `con.commit()`. section 12.0

. . .
import sqlite3                                                          #1
. . .
con = sqlite3.connect("orders.db")                                      #2
cur = con.cursor()
cur.execute(
    "CREATE TABLE IF NOT EXISTS orders(name, drink, flavor, topping);") #3
 
app = Flask(name)
. . .

Inspect the compact setup that imports `sqlite3`, opens the database connection, creates a cursor, and executes the table-creation command. section 12.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Defines the cursor through an analogy and demonstrates creation and execution, while explicitly deferring deeper details.
Examples
Demonstrates CREATE TABLE, INSERT orders, and SELECT-based retrieval in an order-tracking application.
What it assumes
Assumes basic Python application code and database statements, while teaching cursor terminology from zero.
What you can do after
Operate/configure a cursor to execute table-creation, insertion, and selection commands in a SQLite-backed Flask application.
How it connects
Connects cursors with SQLite connections, SQL commands, tables, INSERT, SELECT, and commit().

Also explained in

Compare how 7 books explain cursor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.