Silly/crazy problem with sqlite

Mats Wichmann mats at wichmann.us
Sat Nov 25 14:32:30 EST 2023


On 11/24/23 14:10, Chris Green via Python-list wrote:
> Chris Green <cl at isbd.net> wrote:
>> This is driving me crazy, I'm running this code:-
> 
> OK, I've found what's wrong:-
> 
>>          cr.execute(sql, ('%' + "2023-11" + '%'))
> 
> should be:-
> 
>      cr.execute(sql, ('%' +  x + '%',) )
> 
> 
> I have to say this seems very non-pythonesque to me, the 'obvious'
> default simply doesn't work right, and I really can't think of a case
> where the missing comma would make any sense at all.

as noted, the comma makes it a tuple.

this might be a case where rewriting as an f-string makes it just a 
little more readable, since the syntax will make it look like there's a 
single string followed by a comma - the addition just makes it look less 
clear to my eyes:

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

cr.execute(sql, (f'%{x}%', ))



More information about the Python-list mailing list