Objective
Create a Python program that lets the database itself reject invalid data through constraints, catching sqlite3.IntegrityError. Use an in-memory database with the sqlite3 module (sqlite3.connect(":memory:")).
Instructions
- Create
userswithemail TEXT PRIMARY KEY(primary key: it cannot repeat) andage INTEGER CHECK (age >= 18)(adults only). - Read
nlines withemail age. Try to insert each user insidewith con:. - If it is inserted, show
Registered: email; ifsqlite3.IntegrityErroris raised (repeated email or age under 18) showRejected: email. - At the end show
Users: kand the stored emails, one per line and sorted.
4
a@x.com 20
b@x.com 17
a@x.com 30
c@x.com 18
Registered: a@x.com
Rejected: b@x.com
Rejected: a@x.com
Registered: c@x.com
Users: 2
a@x.com
c@x.com
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise