diff options
author | Munyoki Kilyungi | 2022-09-30 13:39:31 +0300 |
---|---|---|
committer | Munyoki Kilyungi | 2022-09-30 13:39:31 +0300 |
commit | a257c7b0ec996964ed1956bedec36cf9f0b34c23 (patch) | |
tree | 04c3d09b5aa0aafa062637235cb8e251a22e60c6 /topics | |
parent | cc6ef5249ddb77510bd58d88c0761def7235d505 (diff) | |
download | gn-gemtext-a257c7b0ec996964ed1956bedec36cf9f0b34c23.tar.gz |
Add database_connection to example
* topics/maybe-monad.gmi: Add database_connection to example snippet.
Add missing ":" to example.
Diffstat (limited to 'topics')
-rw-r--r-- | topics/maybe-monad.gmi | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/topics/maybe-monad.gmi b/topics/maybe-monad.gmi index b748ad5..d6f87f2 100644 --- a/topics/maybe-monad.gmi +++ b/topics/maybe-monad.gmi @@ -37,15 +37,16 @@ foo.map(lambda x: 1 + x) \ Finally, let's put all this together in a practical example using sql_query_mdict from genenetwork. Consider the following code using the DictCursor. The column foo may contain NULL values, and we need to check for them. ``` -with conn.cursor(MySQLdb.cursors.DictCursor) as cursor: - cursor.execute("SELECT foo FROM bar") - for row in cursor.fetchall(): - if row["foo"] is not None: - print(row["foo"]) +with database_connection() as conn: + with conn.cursor(MySQLdb.cursors.DictCursor) as cursor: + cursor.execute("SELECT foo FROM bar") + for row in cursor.fetchall(): + if row["foo"] is not None: + print(row["foo"]) ``` But, with sql_query_mdict, the row object is a MonadictDict where all values are monadic. We therefore do not need any special conditional checks. ``` -with database_connection() as conn +with database_connection() as conn: for row in sql_query_mdict(conn, "SELECT foo FROM bar"): row["foo"].bind(print) ``` |