1. Save scores in a table
Write code Easy ✓ CompletedWrite a function add_scores(conn, rows) that receives an open connection and a list of (player, points) tuples. It must create the table scores(player TEXT, points INTEGER) if it does not exist, insert every row with executemany and ? placeholders, commit, and return the number of rows in the table.
import sqlite3
def add_scores(conn, rows):
# create the table (IF NOT EXISTS), insert the rows with placeholders, commit
# then return how many rows the table holds
pass
conn = sqlite3.connect(":memory:")
print(add_scores(conn, [("Asha", 30), ("Ben", 20)]))
(?, ?), then SELECT COUNT(*). fetchone()[0] gives the number.import sqlite3
def add_scores(conn, rows):
conn.execute("CREATE TABLE IF NOT EXISTS scores (player TEXT, points INTEGER)")
conn.executemany("INSERT INTO scores (player, points) VALUES (?, ?)", rows)
conn.commit()
return conn.execute("SELECT COUNT(*) FROM scores").fetchone()[0]
conn = sqlite3.connect(":memory:")
print(add_scores(conn, [("Asha", 30), ("Ben", 20)]))