This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: sqlite3 Row can return duplicate keys when using adapters
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, erlendaasland, xtreak
Priority: normal Keywords:

Created on 2014-05-09 21:50 by BreamoreBoy, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg218200 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-05-09 21:50
Code adopted from here https://docs.python.org/3/library/sqlite3.html#default-adapters-and-converters.

import sqlite3
import datetime

con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("create table test(d date, ts timestamp)")

today = datetime.date.today()
now = datetime.datetime.now()

cur.execute("insert into test(d, ts) values (?, ?)", (today, now))

cur.execute('select current_date as "d [date]", current_timestamp as "ts [timestamp]"')
row = cur.fetchone()
print(row.keys())

cur.execute('select current_date as "nit [date]", current_timestamp as "nit [timestamp]"')
row = cur.fetchone()
print(row.keys())

cur.execute('select current_date as " [date]", current_timestamp as " [timestamp]"')
row = cur.fetchone()
print(row.keys())

Output ---

c:\Users\Mark\MyPython>sqlite3_error.py
['d', 'ts']
['nit', 'nit']
['', '']

This clearly defeats the purpose of using keys to access the given columns.  Hardly a show stopper but I thought I'd flag it up.
msg393933 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-05-19 08:08
AFAICS, this has nothing to do with adapters; you'll get duplicate keys just by executing 'select 1 as "token", 2 as "token"'. If you want the columns to have unique names, you should assign them unique names.

At most, there could be a sentence about this in the docs ("best practices"), but IMO this should be obvious. I would leave the docs as they are.

Suggesting to close this as not a bug.
msg398993 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-08-05 10:59
Closing this for now. If anyone disagrees, please reopen :)
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65664
2021-08-05 10:59:35erlendaaslandsetstatus: pending -> closed
resolution: not a bug
messages: + msg398993

stage: resolved
2021-05-19 08:08:37erlendaaslandsetstatus: open -> pending

nosy: + berker.peksag, erlendaasland, - ghaering
messages: + msg393933

assignee: ghaering ->
2018-09-22 16:29:17BreamoreBoysetnosy: - BreamoreBoy
2018-09-22 16:27:42xtreaksetnosy: + xtreak
2015-01-11 02:00:40ghaeringsetassignee: ghaering
2014-05-12 00:06:55pitrousetnosy: + ghaering
2014-05-09 21:50:56BreamoreBoycreate