Use sqlite3 to replace a fragile file of coffee orders with an SQLite database that can handle multiple people writing orders. section 12.0
A small database workflow looks like this:
- Import sqlite3 and connect to orders.db. section 12.0
- Create a cursor so SQL commands can run. section 12.0
- Create an orders table with name, drink, flavor, and topping columns. section 12.0
- Insert each order with placeholders, then commit the change. section 12.0
- Select saved rows and display them in a Flask template. section 12.0
Coffee-shop orders
A Flask app saves each customer's name, drink, flavor, and topping in orders.db, then lists the stored orders in an HTML table. 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 connection, cursor, and CREATE TABLE setup. section 12.0


