classification
Title: sqlite3 package document mistake
Type: behavior Stage:
Components: Documentation Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: berker.peksag, docs@python, erlendaasland, ghaering, seeker-Liu, terry.reedy
Priority: normal Keywords:

Created on 2020-12-12 08:47 by seeker-Liu, last changed 2021-02-12 12:00 by erlendaasland.

Messages (7)
msg382904 - (view) Author: LIU Qingyuan (seeker-Liu) Date: 2020-12-12 08:47
In the document about sqlite3 package, it was suggested that when users are trying to create a table already exists, sqlite3.ProgrammingError is going to be thrown. However, the actual exception thrown is sqlite3.OperationalError, which is inconsistent with the document.

Doc: https://docs.python.org/3/library/sqlite3.html#exceptions
msg383329 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-12-18 22:22
The doc is the same in 3.10.

What OS and exact Python release? Can you post minimal code that verifies your claim?  (Someone what have to verify with 3.10 also.)

This would strike me as an error in the code.  We can fix 'wrong Exception' in the next release.  I am not sure whether we would do so in a current release.
msg383367 - (view) Author: LIU Qingyuan (seeker-Liu) Date: 2020-12-19 10:17
Python 3.9.1 (default, Dec 11 2020, 14:32:07) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> conn = sqlite3.connect("test.db")
>>> conn.execute("CREATE TABLE test (id INTEGER, str TEXT);")
<sqlite3.Cursor object at 0x7ff99f2df3b0>
>>> conn.execute("CREATE TABLE test (id INTEGER, str TEXT);")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: table test already exists
>>> 


I think the reproduction is way too easy and has nothing special so I didn't include one at the very beginning. That's a simple reproduction on interactive console.
msg386859 - (view) Author: Erlend Egeberg Aasland (erlendaasland) * Date: 2021-02-12 11:57
Exception originates from _pysqlite_seterror() in Modules/_sqlite/util.c, line 64:
https://github.com/python/cpython/blob/master/Modules/_sqlite/util.c#L64

Error code 1 (SQLITE_ERROR, https://sqlite.org/rescode.html#error) is a generic sqlite3 error. Changing the default sqlite3 exception for this error code may not be what we want.

Anyway, we should clean up the error handling in the sqlite3 module. There are other inconsistencies apart from this.
msg386860 - (view) Author: Erlend Egeberg Aasland (erlendaasland) * Date: 2021-02-12 11:58
Ref. bpo-24139
msg386861 - (view) Author: Erlend Egeberg Aasland (erlendaasland) * Date: 2021-02-12 11:59
... and also bpo-16379
msg386862 - (view) Author: Erlend Egeberg Aasland (erlendaasland) * Date: 2021-02-12 12:00
*error handling => exception emitted
History
Date User Action Args
2021-02-12 12:00:09erlendaaslandsetmessages: + msg386862
2021-02-12 11:59:14erlendaaslandsetmessages: + msg386861
2021-02-12 11:58:28erlendaaslandsetmessages: + msg386860
2021-02-12 11:57:55erlendaaslandsetmessages: + msg386859
2020-12-19 10:17:20seeker-Liusetmessages: + msg383367
2020-12-18 22:37:53erlendaaslandsetnosy: + erlendaasland
2020-12-18 22:22:23terry.reedysetnosy: + terry.reedy
messages: + msg383329
2020-12-12 09:35:51xtreaksetnosy: + ghaering, berker.peksag
2020-12-12 08:47:49seeker-Liucreate