Mailing List Archive

Re: Silly/crazy problem with sqlite
>

> I really can't think of a case
> where the missing comma would make any sense at all.
>

That is pretty tricky, yes.

The comma means it's a tuple. Without the comma, it's just a string with
parenthesis around it, which is a string.

PyDev console: starting.
Python 3.9.15 (main, Oct 28 2022, 17:28:38) [GCC] on linux
x = ('%' + "2023-11" + '%')
x
'%2023-11%'
x = ('%' + x + '%',)
x
('%%2023-11%%',)
x.__class__.__name__
'tuple'


--
https://mail.python.org/mailman/listinfo/python-list
Re: Silly/crazy problem with sqlite [ In reply to ]
Am 24.11.2023 um 22:49 schrieb Rimu Atkinson via Python-list:
>
>>
>
>> I really can't think of a case
>> where the missing comma would make any sense at all.
>>
>
> That is pretty tricky, yes.
>
> The comma means it's a tuple. Without the comma, it's just a string with
> parenthesis around it, which is a string.
>

Placeholders for the parameters in an SQL command for
sqlite3.execute(..) must always be given as dict or sequence. Even if
it's just one parameter.

Same thing with other database modules, it's given in PEP 249.

HTH
Sibylle

--
https://mail.python.org/mailman/listinfo/python-list
Re: Silly/crazy problem with sqlite [ In reply to ]
On 11/24/2023 4:49 PM, Rimu Atkinson via Python-list wrote:
>
>>
>
>> I really can't think of a case
>> where the missing comma would make any sense at all.
>>
>
> That is pretty tricky, yes.
>
> The comma means it's a tuple. Without the comma, it's just a string with
> parenthesis around it, which is a string.
>
> PyDev console: starting.
> Python 3.9.15 (main, Oct 28 2022, 17:28:38) [GCC] on linux
> x = ('%' + "2023-11" + '%')
> x
> '%2023-11%'
> x = ('%' +  x + '%',)
> x
> ('%%2023-11%%',)
> x.__class__.__name__
> 'tuple'

To make it very clear in your code so that you are reminded next time
you want to re-use it, you could write

param = '%2023-11%'
cr.execute(sql, (param,))

Probably the param value will actually use a variable instead of the
hard-coded value in the example. So use an f-string, because it's more
readable and easier to get right:

date = ... # Where ever the actual date value comes from
param = f'%{date}%'
cr.execute(sql, (param,))

--
https://mail.python.org/mailman/listinfo/python-list