Using namedtuple with sqlite, surely it can do better than the example

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Oct 1 01:23:36 CEST 2014


On 30/09/2014 22:32, cl at isbd.net wrote:
> In the namedtuple documentation there's an example:-
>
>      EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
>
>      import sqlite3
>      conn = sqlite3.connect('/companydata')
>      cursor = conn.cursor()
>      cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
>      for emp in map(EmployeeRecord._make, cursor.fetchall()):
>          print emp.name, emp.title
>
> (I've deleted the csv bit)
>
> Surely having to match up the "name, age, title, department,
> paygrade" between the tuple and the database can be automated, the
> example is rather pointless otherwise.  At the very least one should
> use the same variable instance for both, but it should be possible to
> get the tuple names from the database.
>

I'm not sure what you're trying to achieve but I've found it easy to use 
the sqlite Row object see 
https://docs.python.org/3/library/sqlite3.html#row-objects

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list