Skip to content Skip to sidebar Skip to footer

Python Put Database Records In Namedtuple

I'm trying to write some code in python (2.7) doing this: Open a database in sqlite Do a query to the database, getting some results. The database has more than one table and i ne

Solution 1:

A namedtuple() is nothing but a class generated with a factory function:

SomeRowResult = namedtuple('SomeRowResult', 'var1 var2 var3')

Here SomeRowResult is a class object (a subclass of tuple), and calling it will create instances of the class:

forresultin results:
    result= SomeRowResult(table1.col1, table2.col1, table3.col1)

If you wanted to have a list of these results, you need to explicitly build that list:

all_results = []
forresultin results:
    result= SomeRowResult(table1.col1, table2.col1, table3.col1)
    all_results.append(result)

Post a Comment for "Python Put Database Records In Namedtuple"