File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 13.1 review exercises
2
+
3
+ import sqlite3
4
+
5
+ # Create a temporary database connection in RAM
6
+ with sqlite3 .connect (':memory:' ) as connection :
7
+ c = connection .cursor ()
8
+
9
+ # Create a "Roster" table with Name, Species and IQ fields
10
+ c .execute ("CREATE TABLE Roster(Name TEXT, Species TEXT, IQ INT)" )
11
+
12
+ # Add some data into the database
13
+ roster_data = (
14
+ ("Jean-Baptiste Zorg" , "Human" , 122 ),
15
+ ("Korben Dallas" , "Meat Popsicle" , 100 ),
16
+ ("Ak'not" , "Mangalore" , - 5 )
17
+ )
18
+ c .executemany ("INSERT INTO Roster VALUES(?, ?, ?)" , roster_data )
19
+
20
+ # Update the Species of Korben Dallas to "Human"
21
+ c .execute ("UPDATE Roster SET Species=? WHERE Name=?" ,
22
+ ('Human' , 'Korben Dallas' ))
23
+
24
+ # Display the names and IQs of everyone classified as Human
25
+ c .execute ("SELECT Name, IQ FROM Roster WHERE Species = 'Human'" )
26
+ for row in c .fetchall ():
27
+ print (row )
You can’t perform that action at this time.
0 commit comments