pyodbc - problem passing None as parameter

Frank Millman frank at chagford.com
Fri Oct 23 08:08:55 CEST 2009


Tim Golden wrote:
> Frank Millman wrote:
>>
>>>>> cur.execute('select * from ctrl.dirusers where todate is ?', None)
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module> pyodbc.ProgrammingError: ('42000', 
>> "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax 
>> near @P1'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server 
>> Driver][SQL Server]Statement(s) could not be prepared. (8180)")
>>
>
> I would estimate that it's because you're using
> "where todate is ?" in your WHERE clause, which
> can only possibly be followed by a NULL -- thus making
> it a not-meaningfully parameterisable query.
>

Thanks for the response, Tim.

Why do you say that this is not-meaningfully parameterisable?

I want the final WHERE clause to show 'WHERE todate IS NULL'.

As I showed in my first example, pyodbc has no problem converting "'select 
?', None" into "select NULL". I don't see why this should be any different.

For the record, psycopg2 on Postgresql has no problem with this.

As a workaround, I suppose I could scan the argument list, and if I find a 
None, substitute the ? with NULL in the SQL statement itself.

It would be interesting to view the SQL statement that pyodbc passes to SQL 
Server for execution. Does anyone know if it is possible to set a parameter 
anywhere to enable this?

Thanks

Frank






More information about the Python-list mailing list