liveBook · Manning

Database result set

As explained in Data Analysis with LLMs

A query result is the table of rows and columns produced by executing a database query.

A database result set is the table of rows and columns produced when a database executes a query. It can contain filtered rows, counts, or other aggregate results. section 5.2.2

A result set commonly appears after a query:

  • A SELECT statement chooses columns or aggregates from one or more tables. section 5.2.2
  • FROM identifies the source tables, while WHERE restricts which rows are considered. section 5.2.2
  • Python's sqlite3 cursor retrieves the result rows with fetchall(), returning them as a list. section 5.3.1

Retrieving rows from SQLite

A reusable process_query function connects to the database, creates a cursor, executes the supplied query, retrieves the rows, converts each row to text, and joins the rows with newline separators. section 5.3.1

def process_query(data_path, query):
    with sqlite3.connect(data_path) as connection:  #1
        cursor = connection.cursor()           #2
        cursor.execute(query)    #3
        table_rows = cursor.fetchall()  #4
         #5
        table_strings = [str(r) for r in table_rows]
        return '\n'.join(table_strings)          #6

Follow the complete Python workflow from database connection through cursor execution, fetchall(), row conversion, and formatted output. section 5.3.1

How Data Analysis with LLMs covers it

How deep it goes
Explains result-row retrieval and string formatting, while stopping short of a full SQL introduction.
Examples
Uses filtered game rows, counts of 2017 games, grouped publisher or genre results, and Python row retrieval.
What it assumes
Assumes a database path and query string, while introducing connections, cursors, execution, and result retrieval.
What you can do after
Operate SQLite queries from Python and retrieve their rows as formatted text.
How it connects
Connects SQL queries, SQLite, cursors, structured data, and language-model query translation.

Also explained in

Compare how 4 books explain database result set →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Analysis with LLMs. The section links point to the parts of the book it draws on.