Have SQLite do all the work for you instead:
editor_cursor.execute("""
UPDATE boxes SET x1=CAST(x1/:smallest_box_size as INTEGER),
y1=CAST(y1/:smallest_box_size as INTEGER),
z1=CAST(z1/:smallest_box_size as INTEGER),
x2=CAST(x2/:smallest_box_size as INTEGER),
y2=CAST(y2/:smallest_box_size as INTEGER),
z2=CAST(z2/:smallest_box_size as INTEGER)""",
{'smallest_box_size': smallest_box_size})
In other words, SQLite is perfectly capable of normalizing all the rows for you without piping them all through Python.
The CAST
to INTEGER
will already round a REAL
value, no need to add an explicit round()
call here.
For future reference: you can loop over a result set by iterating over the cursor. No need to call .fetchone()
for each row:
cur.execute("SELECT * FROM boxes")
for row in cur:
# loop will terminate automatically when the rows are exhausted.
This is implemented very efficiently, it only loads results as required to iterate.