Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in _iud_query() when data is set and len(data) > chunksize #3

Open
JamieTaylor-TUOS opened this issue Oct 16, 2023 · 0 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@JamieTaylor-TUOS
Copy link
Contributor

When data is not None and len(data) > chunk_size, the value of affected will be the number of rows affected by the last chunked insert. Instead, need to keep a running total of cursor.rowcount when looping over chunks

def _iud_query(cnx, **kwargs):
"""Execute an insert/update/delete SQL statement."""
chunk_size = kwargs.get("chunk_size", 1000)
data = kwargs.get("data", None)
sqlquery = kwargs.get("sqlquery", None)
debug(f"insert/update/delete query: {sqlquery}")
cursor = cnx.cursor()
if data is None:
cursor.execute(sqlquery)
else:
for i in range(0, len(data), chunk_size):
cursor.executemany(sqlquery, data[i:(i+chunk_size)])
cnx.commit()
affected = cursor.rowcount
cursor.close()
return affected

Suggested fix:

def _iud_query(cnx, **kwargs):
    """Execute an insert/update/delete SQL statement."""
    chunk_size = kwargs.get("chunk_size", 1000)
    data = kwargs.get("data", None)
    sqlquery = kwargs.get("sqlquery", None)
    debug(f"insert/update/delete query: {sqlquery}")
    cursor = cnx.cursor()
    if data is None:
        cursor.execute(sqlquery)
        affected = cursor.rowcount
    else:
        affected = 0
        for i in range(0, len(data), chunk_size):
            cursor.executemany(sqlquery, data[i:(i+chunk_size)])
            affected += cursor.rowcount
    cnx.commit()
    cursor.close()
    return affected
@JamieTaylor-TUOS JamieTaylor-TUOS added the bug Something isn't working label Oct 16, 2023
@JamieTaylor-TUOS JamieTaylor-TUOS self-assigned this Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant