File tree Expand file tree Collapse file tree 2 files changed +14
-0
lines changed Expand file tree Collapse file tree 2 files changed +14
-0
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ response is sent.
3737 :caption: `` flaskr/ db.py``
3838
3939 import sqlite3
40+ from datetime import datetime
4041
4142 import click
4243 from flask import current_app, g
@@ -132,6 +133,11 @@ Add the Python functions that will run these SQL commands to the
132133 init_db()
133134 click.echo(' Initialized the database.' )
134135
136+
137+ sqlite3.register_converter(
138+ " timestamp" , lambda v : datetime.fromisoformat(v.decode())
139+ )
140+
135141:meth: `open_resource() <Flask.open_resource> ` opens a file relative to
136142the ``flaskr `` package, which is useful since you won't necessarily know
137143where that location is when deploying the application later. ``get_db ``
@@ -142,6 +148,10 @@ read from the file.
142148that calls the ``init_db `` function and shows a success message to the
143149user. You can read :doc: `/cli ` to learn more about writing commands.
144150
151+ The call to :func: `sqlite3.register_converter ` tells Python how to
152+ interpret timestamp values in the database. We convert the value to a
153+ :class: `datetime.datetime `.
154+
145155
146156Register with the Application
147157-----------------------------
Original file line number Diff line number Diff line change 11import sqlite3
2+ from datetime import datetime
23
34import click
45from flask import current_app
@@ -44,6 +45,9 @@ def init_db_command():
4445 click .echo ("Initialized the database." )
4546
4647
48+ sqlite3 .register_converter ("timestamp" , lambda v : datetime .fromisoformat (v .decode ()))
49+
50+
4751def init_app (app ):
4852 """Register database functions with the Flask app. This is called by
4953 the application factory.
You can’t perform that action at this time.
0 commit comments