Skip to content

SQLite rowcount is corrupted when combining UPDATE RETURNING w/ table that is dropped and recreated #93421

@zzzeek

Description

@zzzeek

version info:

$ python Python 3.10.0 (default, Nov 5 2021, 17:23:47) [GCC 11.2.1 20210728 (Red Hat 11.2.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sqlite3 >>> sqlite3.sqlite_version '3.36.0' 

we have a test suite that creates a table, runs some SQL, then drops it. if multiple tests run that each perform this task, if the same SQLite connection is used, rowcount starts returning "0". Seems to also require RETURNING to be used. Full demonstration:

importosimportsqlite3defgo(): """function creates a new table, runs INSERT/UPDATE, drops table, commits connection. """# create tablecursor=conn.cursor() cursor.execute( """CREATE TABLE some_table ( id INTEGER NOT NULL, value VARCHAR(40) NOT NULL, PRIMARY KEY (id) ) """ ) cursor.close() conn.commit() # run operationcursor=conn.cursor() cursor.execute( "INSERT INTO some_table (id, value) VALUES (1, 'v1')" ) ident=1cursor.execute( "UPDATE some_table SET value='v2' ""WHERE id=? RETURNING id", (ident,), ) new_ident=cursor.fetchone()[0] assertident==new_identassertcursor.rowcount==1, cursor.rowcountcursor.close() # drop tablecursor=conn.cursor() cursor.execute("DROP TABLE some_table") cursor.close() conn.commit() ifos.path.exists("file.db"): os.unlink("file.db") # passesconn=sqlite3.connect("file.db") go() # run again w/ new connection (same DB), passesconn=sqlite3.connect("file.db") go() print("FAILURE NOW OCCURS") # run again w/ same connection, failsgo()

on the third run, where we ran the "test" on the same connection twice, it fails:

$ python test3.py FAILURE NOW OCCURS Traceback (most recent call last): File "/home/classic/dev/sqlalchemy/test3.py", line 62, in <module> go() File "/home/classic/dev/sqlalchemy/test3.py", line 39, in go assert cursor.rowcount == 1, cursor.rowcount AssertionError: 0 

it would appear there's some internal caching of table state that needs to be cleared.

Metadata

Metadata

Labels

3.11only security fixes3.12only security fixestopic-sqlite3type-bugAn unexpected behavior, bug, or error

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions