Adding multiple records
Date: March 11th 2016
Last updated: March 11th 2016
Adding multiple records to a table had me confused. I was confused because the original table I created (scoreboard) was not set to autoincrement. Therefore, in this notebook entry I am recreating the scoreboard table and adding data to it. One of the important things to note here is the executemany statement which reads through a list of tuples.
import sqlite3 # connect con = sqlite3.connect('gitbook.db') # cursor cur = con.cursor() # drop scoreboard table cur.execute('DROP TABLE IF EXISTS scoreboard') # create table again cur.execute('CREATE TABLE scoreboard (id INTEGER PRIMARY KEY AUTOINCREMENT, \ score REAL NOT NULL, \ name TEXT DEFAULT player)') # data data = [(10.0,), (12.1,), (32.5,),] # execute cur.executemany('INSERT INTO scoreboard (score) VALUES (?)', data) # commit con.commit() # close con.close()