DateClassRowFactory stop/start

38 views
Skip to first unread message

jose isaias cabrera

unread,
Mar 13, 2025, 3:52:57 PMMar 13
to python-sqlite
Greetings.

I have set the ext.DateClassRowFactory to be used globally. How do I stop this for a few lines of codes? For example,
<code>
  mon = []
  for m in months:
    mm = m.replace('-','')
    nq = 'M' + mm
    mon.append(nq)
  mon.sort()
  ...
  for row in con.execute(q):
  ...
    h.append(DisplayProjectData(row.ProjID))
  ...
    #stop apsw.ext.DataClassRowFactory here
    for m in mon:
      hc = str(row[m])
      if hc < need:
        ...
      else:
        ...
    #start apsw.ext.DataClassRowFactory again here
    h.append(row.pm)
    ...
</code>

How do I stop and start DateClassRowFactory? Thanks.

josé

Roger Binns

unread,
Mar 13, 2025, 4:02:46 PMMar 13
to Python-SQLite group
On Thu, 13 Mar 2025, at 12:51, jose isaias cabrera wrote:
> I have set the ext.DateClassRowFactory to be used globally. How do I
> stop this for a few lines of codes?

You can set a row tracer on a cursor that does nothing.

cursor = connection.cursor()
cursor.row_trace = lambda _, row: row

for row in cursor.execute("..."):
# row is sqlite tuple here
print(f"{row=}")

Row tracers on a cursor override the connection row tracer, but they have to be set to a non-None value.

Roger

jose isaias cabrera

unread,
Mar 14, 2025, 9:52:33 AMMar 14
to python-sqlite

Totally lost with what you said. Way over my head. Sorry.  I am trying to change the code from python sqlite3 library to apsw. All is well, except that I can do this in sqlite3:

for row in cur.execute(q):
    print(row[1])
    # and also address the same field by name
    print(row["NameOfFieldOne"]

and that is really easy. Is there a way to do this in apsw without having to use the DataClassRowFactory? I was looking in the doc, and could not find anything like it, but I must confess, I have not read it all.

Roger Binns

unread,
Mar 14, 2025, 10:37:01 AMMar 14
to Python-SQLite group
> All is well except that I can do this in sqlite3:

Thanks for clarifying your requirements. See https://rogerbinns.github.io/apsw/tips.html#query-patterns

> print(row[1])
> # and also address the same field by name
> print(row["NameOfFieldOne"]

If you still want to do exactly that then use this as your row trace

class DictRow(dict):
def __init__(self, cursor, row):
col_names = [description[0] for description in cursor.getdescription()]
super().__init__(zip(col_names, row))
self.row_tuple = row

def __getitem__(self, key):
if isinstance(key, int):
return self.row_tuple[key]
return dict.__getitem__(self, key)

connection.row_trace = DictRow

Roger

jose isaias cabrera

unread,
Mar 14, 2025, 6:55:56 PMMar 14
to python-sqlite

Thanks, Roger. Works perfectly. 

A thought: perhaps this could be in the Example/Tour with some comments as "...allow easy conversion of sqlite3 library to apsw".

Roger Binns

unread,
Mar 14, 2025, 7:10:12 PMMar 14
to Python-SQLite group
> A thought: perhaps this could be in the Example/Tour with some comments
> as "...allow easy conversion of sqlite3 library to apsw".

The query patterns tip covers the best ways of structuring queries.

The sqlite3 pattern best matches how other databases work where they do track origin tables and columns names more robustly, which is why sqlite3 copies that pattern. But it doesn't match how SQLite works and isn't a good approach for SQLite.

That said, you are the first to ask this, and I generally wait till at least a few more do, especially to get an understanding of what the underlying issue being addressed is.

If I had a magic wand, what I really want is a way of expressing the query that automatically made corresponding variables spring to light, didn't require duplication inside the SQL and in the Python, and could be checked by automatic tools.

Another approach is the aiosql package which encourages queries to be in a separate version controlled file:

https://nackjicholson.github.io/aiosql/defining-sql-queries.html

Hopefully the Python community will figure out something straightforward in the future.

Roger

jose isaias cabrera

unread,
Mar 15, 2025, 7:38:49 PMMar 15
to python-sqlite

Fair enough, Roger. Thanks for the support and suggestions.
Reply all
Reply to author
Forward
0 new messages