Pyodbc row to list

pyodbc row to list

When working with pyodbc, you can convert a row object into a list using the list() function. This function converts an iterable (such as a row object) into a list by iterating over its elements and adding them to a new list.

Example:

# Assuming you have a pyodbc row object 'row'
import pyodbc

# Assuming you have a pyodbc connection 'conn' established
cursor = conn.cursor()
cursor.execute("SELECT * FROM my_table")
row = cursor.fetchone()

# Converting the pyodbc row object into a list
row_list = list(row)
print(row_list)

In this example, we assume that you have established a pyodbc connection and cursor, and executed a query to fetch a single row from a table.

The fetchone() method returns a single row from the result set as a row object. We then use the list() function to convert the row object row into a list and store it in the variable row_list.

Finally, we print the row_list which will display the elements of the row in a list format.

Leave a comment