cardinal_pythonlib.dbfunc


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Functions to operate with the raw Python database API.

See https://www.python.org/dev/peps/pep-0249/.

cardinal_pythonlib.dbfunc.dictfetchall(cursor: cardinal_pythonlib.typing_helpers.Pep249DatabaseCursorType) → List[Dict[str, Any]][source]

Return all rows from a cursor as a list of OrderedDict objects.

Parameters:cursor – the cursor
Returns:a list (one item per row) of OrderedDict objects whose key are column names and whose values are the row values
cardinal_pythonlib.dbfunc.dictfetchone(cursor: cardinal_pythonlib.typing_helpers.Pep249DatabaseCursorType) → Optional[Dict[str, Any]][source]

Return the next row from a cursor as an OrderedDict, or None.

cardinal_pythonlib.dbfunc.fetchallfirstvalues(cursor: cardinal_pythonlib.typing_helpers.Pep249DatabaseCursorType) → List[Any][source]

Return a list of the first value in each row.

cardinal_pythonlib.dbfunc.gendicts(cursor: cardinal_pythonlib.typing_helpers.Pep249DatabaseCursorType, arraysize: int = 1000) → Generator[Dict[str, Any], None, None][source]

Generate all rows from a cursor as OrderedDict objects.

Parameters:
  • cursor – the cursor
  • arraysize – split fetches into chunks of this many records
Yields:

each row, as an OrderedDict whose key are column names and whose values are the row values

cardinal_pythonlib.dbfunc.genfirstvalues(cursor: cardinal_pythonlib.typing_helpers.Pep249DatabaseCursorType, arraysize: int = 1000) → Generator[Any, None, None][source]

Generate the first value in each row.

Parameters:
  • cursor – the cursor
  • arraysize – split fetches into chunks of this many records
Yields:

the first value of each row

cardinal_pythonlib.dbfunc.genrows(cursor: cardinal_pythonlib.typing_helpers.Pep249DatabaseCursorType, arraysize: int = 1000) → Generator[List[Any], None, None][source]

Generate all rows from a cursor.

Parameters:
  • cursor – the cursor
  • arraysize – split fetches into chunks of this many records
Yields:

each row

cardinal_pythonlib.dbfunc.get_fieldnames_from_cursor(cursor: cardinal_pythonlib.typing_helpers.Pep249DatabaseCursorType) → List[str][source]

Get a list of fieldnames from an executed cursor.