apsw icon indicating copy to clipboard operation
apsw copied to clipboard

Implement C equivalent of sqlite3.Row

Open rogerbinns opened this issue 10 years ago • 4 comments

From rogerbinns on December 21, 2012 10:48:09

Basically produce a named tuple that can be used as a Row tracer.

Original issue: http://code.google.com/p/apsw/issues/detail?id=137

rogerbinns avatar Dec 29 '13 03:12 rogerbinns

I know this is an old issue but is there a recommended way to get named column access to results?

lllama avatar Sep 14 '22 10:09 lllama

There is an example in the tips, making the result be a dictionary:

https://rogerbinns.github.io/apsw/tips.html#customizing-cursors

Do you have a preferred type for return other than a dictionary? I am thinking of updating the doc to show returning named tuple or dataclass

rogerbinns avatar Sep 14 '22 14:09 rogerbinns

I'm currently creating dataclasses by doing MyClass(*result) so that would be my preference.

(I'm actually using aiosql to call my queries, so I will have to see whether I can drill down to the cursor for that - but that's not important for this issue.)

lllama avatar Sep 20 '22 09:09 lllama

The way I am going to proceed is adding an apsw.ext module that has row tracers that can produce dicts, namedtuple, and dataclass. I did a quick proof of concept and found that the hard part is processing the column names to deal with duplicates and renaming (eg keywords like continue have to be changed to something else). Hopefully in the next release.

rogerbinns avatar Sep 20 '22 14:09 rogerbinns

Shipped

rogerbinns avatar Nov 29 '22 22:11 rogerbinns