This MySQL Get Started lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
MySQL stores rows in tables. Python talks to it through a driver, which this browser runner does not include.
The lessons use a list of dicts so you can see the row shape a query returns.
Example 1: Start here
Install a driver such as mysql-connector-python in a virtual environment, and keep the password out of the source file.
rows = [{"id": 1, "city": "Chennai"}, {"id": 2, "city": "Madurai"}] # a list; the values stay in this order
print(rows[0])
Example 2: Filter the rows
MySQL Get Started is easier to see as a list of records. The filter keeps every row whose seats value is at least 10.
rows = [{"topic": "MySQL Get Started", "seats": 17}, {"topic": "SQL", "seats": 12}] # a list; the values stay in this order
print([row["topic"] for row in rows if row["seats"] >= 10]) # keep the items that pass the test
Example 3: Update one field
An update changes one field and leaves the rest of the record alone. On a database you would run an UPDATE or an update_one for the same change.
row = {"topic": "MySQL Get Started", "seats": 17} # a dictionary; each key has one value
row["seats"] = row["seats"] + 1 # change this one field
print(row)
Example 4: Count the matches
Counting matches is the idea behind COUNT(*). Here the number 17 appears twice.
rows = [17, 4, 17] # a list; the values stay in this order
print(sum(1 for value in rows if value == 17)) # keep the items that pass the test
