| title | Python |
|---|---|
| tagline | Using databases from Python. |
[%inc basic_python_query.py %] [%inc basic_python_query.out %]
sqlite3is part of Python's standard library- Create a connection to a database file
- Get a [%g cursor "cursor" %] by executing a query
- More common to create cursor and use that to run queries
- Fetch all rows at once as list of tuples
[%inc incremental_fetch.py %] [%inc incremental_fetch.out %]
cursor.fetchonereturnsNonewhen no more data- There is also
fetchmany(N)to fetch (up to) a certain number of rows
[%inc insert_delete.py %] [%inc insert_delete.out %]
- Each
executeis its own transaction
[%inc interpolate.py %] [%inc interpolate.out %]
- From [XKCD][xkcd-tables]
[% figure slug="python_xkcd" img="xkcd_327_exploits_of_a_mom.png" alt="XKCD cartoon showing a mother scolding a school for not being more careful about SQL injection attacks" caption="XKCD Exploits of a Mom" %]
Write a Python script that takes island, species, sex, and other values as command-line arguments and inserts an entry into the penguins database.
[%inc script_execution.py %] [%inc script_execution.out %]
- But what if something goes wrong?
[%inc exceptions.py %] [%inc exceptions.out %]
[%inc embedded_python.py %] [%inc embedded_python.out %]
- SQLite calls back into Python to execute the function
- Other databases can run Python (and other languages) in the database server process
- Be careful
[%inc dates_times.py %] [%inc dates_times.out %]
sqlite3.PARSE_DECLTYPEStellssqlite3library to use converts based on declared column types- Adapt on the way in, convert on the way out
Write a Python adapter that truncates real values to two decimal places as they are being written to the database.
[%inc install_jupysql.sh %]
- And then inside the notebook:
[%inc load_ext.text %]
- Loads extension
[%inc jupyter_connect.text %] [%inc jupyter_connect.out %]
- Connects to database
sqlite://with two slashes is the protocol/data/penguins.db(one leading slash) is a local path
- Single percent sign
%sqlintroduces one-line command - Use double percent sign
%%sqlto indicate that the rest of the cell is SQL
[%inc jupyter_select.text %] [%inc jupyter_select.out %]
| species | num |
|---|---|
| Adelie | 152 |
| Chinstrap | 68 |
| Gentoo | 124 |
[%inc install_pandas.sh %] [%inc select_pandas.py %] [%inc select_pandas.out %]
- Be careful about datatype conversion when using [Pandas][pandas]
Write a command-line Python script that uses Pandas to re-create the penguins database.
[%inc install_polars.sh %] [%inc select_polars.py %] [%inc select_polars.out %]
- The [%g uri "Uniform Resource Identifier" %] (URI) specifies the database
- The query is the query
- Use the ADBC engine instead of the default ConnectorX with [Polars][polars]
Write a command-line Python script that uses Polars to re-create the penguins database.
[%inc orm.py %] [%inc orm.out %]
- An [%g orm "object-relational mapper" %] (ORM) translates table columns to object properties and vice versa
- [SQLModel][sqlmodel] relies on Python type hints
Write a command-line Python script that uses SQLModel to re-create the penguins database.
[%inc orm_relation.py mark=keep %] [%inc orm_relation.out %]
- Make foreign keys explicit in class definitions
- SQLModel automatically does the join
- The two staff with no department aren't included in the result