|
| 1 | +More Database Practice |
| 2 | +------------------------ |
| 3 | +.. parsonsprob:: db_create_and_insert_new_hptest |
| 4 | + :adaptive: |
| 5 | + :numbered: left |
| 6 | + :order: 10, 5, 9, 0, 11, 6, 3, 4, 1, 7, 8, 2 |
| 7 | + |
| 8 | + Put the code blocks in order to import the needed module, create the connection, create the cursor, create the table if it does not exist, insert data into the table if it isn't already there, commit the changes, and then select the data and print it. Finally close the cursor. |
| 9 | + ----- |
| 10 | + import sqlite3 |
| 11 | + ===== |
| 12 | + conn = sqlite3.connect('music.sqlite') |
| 13 | + ===== |
| 14 | + cur = conn.cursor() |
| 15 | + ===== |
| 16 | + # create the table with a key and unique title |
| 17 | + cur.execute('CREATE TABLE IF NOT EXISTS Tracks |
| 18 | + (id INTEGER PRIMARY KEY, title TEXT UNIQUE, |
| 19 | + plays INTEGER)') |
| 20 | + ===== |
| 21 | + # create the table with a key and unique title |
| 22 | + cur.execute('CREATE TABLE Tracks |
| 23 | + (id INTEGER PRIMARY KEY, title TEXT UNIQUE, |
| 24 | + plays INTEGER)') #paired |
| 25 | + ===== |
| 26 | + # insert data into the Tracks table |
| 27 | + cur.execute('INSERT OR IGNORE INTO Tracks |
| 28 | + (title, plays) VALUES (?, ?)', |
| 29 | + ('Thunderstruck', 20)) |
| 30 | + cur.execute('INSERT OR IGNORE INTO Tracks |
| 31 | + (title, plays) VALUES (?, ?)', |
| 32 | + ('My Way', 15)) |
| 33 | + ===== |
| 34 | + # commit the changes |
| 35 | + conn.commit() |
| 36 | + ===== |
| 37 | + print('Tracks:') |
| 38 | + cur.execute('SELECT title, plays FROM Tracks') |
| 39 | + ===== |
| 40 | + print('Tracks:') |
| 41 | + conn.execute('SELECT title, plays FROM Tracks') #paired |
| 42 | + ===== |
| 43 | + for row in cur: |
| 44 | + ===== |
| 45 | + print(row) |
| 46 | + ===== |
| 47 | + # close the cursor |
| 48 | + cur.close() |
| 49 | + |
| 50 | + |
| 51 | +.. fillintheblank:: db_create_table_if_not_exists_hptest |
| 52 | + :casei: |
| 53 | + |
| 54 | + Fill in the three blanks required to create the table only if it doesn't exist already |
| 55 | + |
| 56 | + .. code-block:: python |
| 57 | +
|
| 58 | + cur.execute(''' |
| 59 | + CREATE TABLE [_____][_____][_____] Twitter |
| 60 | + (name TEXT, retrieved INTEGER, friends INTEGER) |
| 61 | + ''') |
| 62 | +
|
| 63 | + - :IF: Correct! |
| 64 | + :.*: Try again! |
| 65 | + - :NOT: Correct! |
| 66 | + :.*: Try again! |
| 67 | + - :EXISTS: Correct! |
| 68 | + :.*: Try again! |
| 69 | + |
| 70 | +.. hparsons:: db_limit_one_hptest |
| 71 | + :language: sql |
| 72 | + :dburl: /_static/hptest.db |
| 73 | + :randomize: |
| 74 | + |
| 75 | + Arrange the blocks to select names from the table Twitter, and return just one row. |
| 76 | + |
| 77 | + ~~~~ |
| 78 | + --blocks-- |
| 79 | + SELECT |
| 80 | + name |
| 81 | + FROM |
| 82 | + Twitter |
| 83 | + LIMIT |
| 84 | + 1 |
| 85 | + WHERE |
| 86 | + 2 |
| 87 | + --unittest-- |
| 88 | + assert 0,0 == Alice |
| 89 | + assert 1,0 == NO_DATA |
| 90 | + |
| 91 | + |
| 92 | +.. fillintheblank:: db_update_and_set_hptest |
| 93 | + :casei: |
| 94 | + |
| 95 | + Fill in the two blanks to specify that you want to modify the row where name = acct and want to change the value of retrieved to 1. |
| 96 | + |
| 97 | + .. code-block:: python |
| 98 | +
|
| 99 | + cur.execute('[_____] Twitter [_____] retrieved=1 WHERE name = ?', (acct, )) |
| 100 | +
|
| 101 | + - :UPDATE: Correct! |
| 102 | + :.*: Try again! |
| 103 | + - :SET: Correct! |
| 104 | + :.*: Try again! |
| 105 | + |
| 106 | +.. fillintheblank:: db_insert_into_twitter_fitb_hptest |
| 107 | + :casei: |
| 108 | + |
| 109 | + Fill in the two blanks to add a new row of data to the Twitter table. |
| 110 | + |
| 111 | + .. code-block:: python |
| 112 | +
|
| 113 | + cur.execute(''' |
| 114 | + [_____] [_____] Twitter (name, retrieved, friends) |
| 115 | + VALUES (?, 0, 1)''', (screenName, )) |
| 116 | +
|
| 117 | + - :INSERT: Correct! |
| 118 | + :.*: Try again! |
| 119 | + - :INTO: Correct! |
| 120 | + :.*: Try again! |
| 121 | + |
| 122 | + |
| 123 | +.. fillintheblank:: db_id_primary_key_fitb_hptest |
| 124 | + :casei: |
| 125 | + |
| 126 | + Fill in the two blanks to make id a primary key (each row will have a unique value for id). |
| 127 | + |
| 128 | + .. code-block:: python |
| 129 | +
|
| 130 | + cur.execute(''' |
| 131 | + CREATE TABLE People |
| 132 | + (id INTEGER [_____] [_____], name TEXT UNIQUE, retrieved INTEGER) |
| 133 | + ''') |
| 134 | +
|
| 135 | + - :PRIMARY: Correct! |
| 136 | + :.*: Try again! |
| 137 | + - :KEY: Correct! |
| 138 | + :.*: Try again! |
| 139 | + |
| 140 | + |
| 141 | +.. fillintheblank:: db_id_insert_or_ignore_hptest |
| 142 | + :casei: |
| 143 | + |
| 144 | + Fill in the two blanks to only execute the following insert if it does not violate any constraints, such as not allowing duplicate data. |
| 145 | + |
| 146 | + .. code-block:: python |
| 147 | +
|
| 148 | + cur.execute(''' |
| 149 | + INSERT [_____] [_____] INTO People (name, retrieved) |
| 150 | + VALUES (?, 0)''', (friend, ) ) |
| 151 | + ''') |
| 152 | +
|
| 153 | + - :OR: Correct! |
| 154 | + :.*: Try again! |
| 155 | + - :IGNORE: Correct! |
| 156 | + :.*: Try again! |
| 157 | +
|
| 158 | +
|
| 159 | +.. fillintheblank:: db_unique_tuple_hptest |
| 160 | + :casei: |
| 161 | +
|
| 162 | + Fill in the two blanks to only allow tuples of ``from_id`` and ``to_id`` that are unique. |
| 163 | +
|
| 164 | + .. code-block:: python |
| 165 | +
|
| 166 | + cur.execute(''' |
| 167 | + CREATE TABLE IF NOT EXISTS Follows |
| 168 | + (from_id INTEGER, to_id INTEGER, [_____](from_id, [_____]))''') |
| 169 | +
|
| 170 | + - :UNIQUE: Correct! |
| 171 | + :.*: Try again! |
| 172 | + - :to_id: Correct! |
| 173 | + :.*: Try again! |
| 174 | +
|
| 175 | +
|
| 176 | +.. hparsons:: db_select_retrieved_zero_fitb_hptest |
| 177 | + :language: sql |
| 178 | + :dburl: /_static/hptest.db |
| 179 | + :randomize: |
| 180 | +
|
| 181 | + Arrange the blocks to select ``id`` and ``name`` from the table ``People`` where that person's friends have not been retrieved yet. |
| 182 | + ~~~~ |
| 183 | + --blocks-- |
| 184 | + SELECT |
| 185 | + id,name |
| 186 | + FROM |
| 187 | + People |
| 188 | + WHERE |
| 189 | + retrieved |
| 190 | + = 0 |
| 191 | + name |
| 192 | + ON |
| 193 | + --unittest-- |
| 194 | + assert 0,0 == 2 |
| 195 | + assert 0,1 == Pear |
| 196 | + assert 1,0 == 3 |
| 197 | + assert 1,1 == Watermelon |
0 commit comments