Learn

Nice job! We have learned how to create, insert, edit, and pull specific data from a SQLite database. However, we need to learn one of the most important methods! If we create or edit a data table using SQLite, we MUST use the .commit() method to save any alteration made to the database. If we do not commit these changes, they can be lost!

# Insert row into toys table cursor.execute('''INSERT INTO toys VALUES (2244560, 'Ultimate Ninja Fighter', 24.99, 'action')''')
# commit changes to database connection.commit()

In the example above, a row was inserted into the toys data table and then immediately following, committed the alteration to the database. Committing the changes ensures that others who view the database will also see these changes.

Notice that we used the .commit() method in combination with the connection object.

Closing the Connection

Once we have committed all changes, we may close the connection to the database with .close(). This will ensure that we do not edit the wrong database the next time we use SQLite.

# close connection connection.close()

Notice again that we used the .close() method with the connection object.

Instructions

1.

The module sqlite3, connection object, and cursor object are already in your text editor.

Use the SQL commands INSERT INTO table_name VALUES to insert this row, ('Stephanie Bready', 37, 'stephB423', 30.00) into new_table.

After doing this, commit the change to the database using the .commit() method.

2.

With your connection object con, commit the change to the database using the .commit() method.

3.

Close the connection using the connection object and the method .close().

As a bonus exercise, after closing the database, try to use the .execute() method on curs. What happens?

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?