Block 121: SQLite Basics with Python
Create databases and tables from Python.
Concepts
- import sqlite3
- conn = sqlite3.connect('mydb.db')
- cursor.execute() with CREATE TABLE, INSERT, SELECT
- conn.commit() and conn.close()
- Context managers with 'with' statement
Code Examples
See exercise below.
Exercise
Create a database with a 'students' table (id, name, grade). Insert 5 rows. Query all rows and print them.
Homework
What is the difference between conn.commit() and conn.close()? What happens if you forget commit()?