Great job! Now that you can
CREATE a SQLite data table and
INSERT a single row, let’s insert multiple rows of data with the
In the example below, an object
new_students contains a list of the rows we want to insert. These rows follow the same table schema of the already existing
# Insert multiple values into table at once new_students = [(102, 'Joe', 32, '2022-05-16', 'Pass'), (103, 'Stacy', 10, '2022-05-16', 'Pass'), (104, 'Angela', 21, '2022-12-20', 'Pass'), (105, 'Mark', 21, '2022-12-20', 'Fail'), (106, 'Nathan', 21, '2022-12-20', 'Pass') ] # Insert values into the students table cursor.executemany('''INSERT INTO students VALUES (?,?,?,?,?)''', new_students)
This last line of code uses the same SQL commands
INSERT INTO table_name VALUES that we used in the previous exercise. However, within parentheses, there is a list of question marks that act as field placeholders. The five question marks represent each of the five fields in the database we are inserting values into. Lastly, we include the object
new_students at the end of the
sqlite3, connection object, and cursor object are already in your text editor.
Notice that the object
new_rows is in your text editor.
new_rows has a tuple list with the rows of data that you will insert into
.executemany() method, insert
new_rows into the table