Silly/crazy problem with sqlite

Thomas Passin list1 at tompassin.net
Sat Nov 25 18:40:58 EST 2023


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,))



More information about the Python-list mailing list