-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
sqlite3.connect(autocommit=False)
breaks executescript("BEGIN; COMMIT")
#112441
Comments
This is intended behaviour; https://docs.python.org/3/library/sqlite3.html#sqlite3-transaction-control-autocommit |
No. |
Hi @erlend-aasland, thanks for your reply. I see now that my proposed fix is unsuitable. And I missed that apparently during the deprecation period there will be a warning unless autocommit is explicitly set to True or False (source), which should address the backward compatibility issue. Can you please share your thoughts on the best way to use Here are the options I can think of: A. Use B. Use So should I use I've also come up with the following, which works with both the current default and the future default, however it's a horrible hack and will produce a warning during the deprecation period(?) because it doesn't explicitly set autocommit: import sqlite3
dump = "BEGIN TRANSACTION; ...; COMMIT"
con = sqlite3.connect("restored.sqlite")
if con.in_transaction():
con.execute("COMMIT")
con.executescript(dump)
con.execute("BEGIN")
...
con.close() |
For that scenario you cannot use the implicit transaction control recommended by PEP-249. You can do that either by setting kdws = {"isolation_level": None} if legacy else {"autocommit": True}
cx = sqlite3.connect(path, **kwds)
cx.executescript(sql_with_tx_ctrl_statements) It's up to you how you set
Currently,
I don't see how this is an issue; the docs recommend the default that PEP-249 recommends. If your application's requirements don't align with that recommendation, that's ok. It is ok to use |
Many thanks! |
Bug report
Bug description:
Background: The autocommit parameter was added to the sqlite3 module in Python 3.12 (#83638), the default is currently LEGACY_TRANSACTION_CONTROL but it is set to change to False in a future Python release.
When autocommit is False, it breaks code that uses executescript() with transaction control. Example:
This program works on all existing Python versions (2.7 - 3.13.0a1), but with autocommit=False it throws the following error:
So making autocommit=False the default in future will break backward compatibility.
One idea for fixing this is to change the behaviour of executescript() so that when autocommit is False, it will first execute an implicit COMMIT (as it already does when autocommit=LEGACY_TRANSACTION_CONTROL), and after it will execute BEGIN.Edit: This idea is unsuitable, because it is incompatible with the model where a transaction is always open.The text was updated successfully, but these errors were encountered: