Source code for getml.sqlite3.to_list

# Copyright 2022 The SQLNet Company GmbH
#
# This file is licensed under the Elastic License 2.0 (ELv2).
# Refer to the LICENSE.txt file in the root of the repository
# for details.
#


"""
Returns a table as a list of lists.
"""

import sqlite3

# ----------------------------------------------------------------------------


def _handle_query(query):
    if "SELECT" in query:
        return query
    return 'SELECT * FROM "' + query + '";'


# ----------------------------------------------------------------------------


[docs]def to_list(conn, query): """ Transforms a query or table into a list of lists. Returns a tuple which contains the column names and the actual data. Args: conn: A sqlite3 connection created by :func:`~getml.sqlite3.connect`. query (str): The query used to get the table. You can also pass the name of the table, in which case the entire table will be imported. """ # ------------------------------------------------------------ if not isinstance(conn, sqlite3.Connection): raise TypeError("'conn' must be an sqlite3.Connection object") if not isinstance(query, str): raise TypeError("'query' must be a str") # ------------------------------------------------------------ query = _handle_query(query) cursor = conn.execute(query) colnames = [description[0] for description in cursor.description] data = cursor.fetchall() return colnames, data