You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tasks.md
+36-7
Original file line number
Diff line number
Diff line change
@@ -131,19 +131,48 @@ In the body of the `get_db` function use the built-in `getattr()` function to ge
131
131
132
132
## 3.6 - sqlite3 Row Factory
133
133
134
-
@pytest.mark.app-sqlite3-row-factory To make accessing data easier, after the if statement in `get_db`, set the row_factory of `db` to `sqlite3.Row`. All rows returned from the database will be named tuples.
134
+
@pytest.mark.app-sqlite3-row-factory To make accessing data easier, after the if statement in `get_db`:
135
+
- Set the row_factory of `db` to `sqlite3.Row`. **Note: All rows returned from the database will be named tuples.**
136
+
- Return the `db` variable.
135
137
136
-
Return the `db` variable.
138
+
## 3.7 - Query Database Function
137
139
138
-
## 3.7 - Querying the Database
140
+
@pytest.mark.app-query-database-function Let’s create a function to make it easier to query the database.
139
141
140
-
@pytest.mark.app-querying-the-database Let’s create a function below the `get_db` function to make it easier to query the database.
142
+
Below the `get_db` function in `app.py` create a function called `query_db`.
141
143
142
-
Call the function `query_db`, have the function accept three parameters: `query`, `args`, and `one`. Set the default of `args` to an empty tuple `()`. Set the default of `one` to `False`. Call the newly created `get_db` function and assign the return value to a `db`variable. Call the `execute` function, passing in the `query` and `args` variables, on the `db` and assign the return value to a variable called `cursor`. `fetchall()` data from the `cursor` and assign it to a variable called `results`. Close the `cursor` with the `close` function. Next test if there are `results` else return `None`. If there are results test if `one` is `True` and return `results[0]` else return `results`.
144
+
In the body of `query_db` create a variable called `db`. Assign this variablethe return value of a call to the newly created `get_db` function.
143
145
144
-
## 3.5 - Close the Connection
146
+
## 3.8 - Query Database Function Parameters
145
147
146
-
@pytest.mark.app-close-the-connection In order to make sure the database connection is closed when the `app_context` is torn down create a function in `app.py` called `close_connection`. Add a parameter called `exception`. In the body of the function use the `getattr` function to get the `_database` attribute from the `g` object. If `_database` doesn’t exist set the default to `None`. Assign the return value of the `getattr` function to`db`. If `db` is not `None``close` the `db`. To ensure this function is called when the `app_context` is destroyed use the ``@app.teardown_appcontext` decorator.
148
+
@pytest.mark.app-query-database-function-parameters Still working with the `query_db` function:
149
+
- Add three parameters: `query`, `args`, and `one`.
150
+
- Set the default of `args` to an empty tuple `()`.
151
+
- Set the default of `one` to `False`.
152
+
153
+
## 3.9 - Query Database Function Execute
154
+
@pytest.mark.app-query-database-function-execute In the body of `query_db` call the `execute` function on `db`, pass in the `query` and `args` variables. Assign the return value to a variable called `cursor`.
155
+
156
+
## 3.10 - Query Database Function Execute
157
+
@pytest.mark.app-query-database-function-fetchall In the body of `query_db`:
158
+
-`fetchall` data from the `cursor` and assign it to a variable called `results`.
159
+
- Close the `cursor` with the `close` function.
160
+
161
+
## 3.11 - Query Database Function Execute
162
+
@pytest.mark.app-query-database-function-single Next, in the function body add a test if `one` is `True`:
163
+
- if true return a ternary if, `results[0] if results else None`.
164
+
- else return all `results`.
165
+
166
+
## 3.12 - Close the Connection
167
+
168
+
@pytest.mark.app-close-the-connection In order to make sure the database connection is closed when the `app_context` is torn down create a function in `app.py` called `close_connection`.
169
+
Add a parameter called `exception` to the parameter list.
170
+
171
+
In the function body:
172
+
- Call `getattr` with three arguments `g`, `'_database'`, and `None` and assign the return value to a `db` variable.
173
+
- If `db` is not `None``close` the `db`.
174
+
175
+
To ensure this function is called when the `app_context` is destroyed use the `@app.teardown_appcontext` decorator.
0 commit comments