[Tutor] sqlite query problem

Hugo Arts hugo.yoshi at gmail.com
Fri Jan 29 17:53:52 CET 2010


2010/1/29 BOBÁK Szabolcs <szabolcs.bobak at gmail.com>:
>
> This also works, but this not:
> sql_command_stat = 'SELECT COUNT(lastmoddate) FROM
> '+sql_tablename_orig+'WHERE lastmoddate < '+str(lastmod_date1)
> sql_cursor.execute(sql_command_stat)
>
> This was my original try, but tried various in various formula.
> sql_command_stat = 'SELECT COUNT(lastmoddate) FROM
> '+sql_tablename_orig+'WHERE lastmoddate < %d'
> sql_cursor.execute(sql_command_stat, %lastmod_date1)
>
> sql_command_stat = 'SELECT COUNT(lastmoddate) FROM
> '+sql_tablename_orig+'WHERE lastmoddate < (?)'
> sql_cursor.execute(sql_command_stat, (lastmod_date1))
>
> But always the same error message:
> sqlite3.OperationalError: near "<": syntax error
> File "C:\python\stat.py", line 42, in <module>
> sql_cursor.execute(sql_command_stat)
>
> From the three attempt I concluded that it's the same if I pass the variable
> value as a string or an integer (maybe I am wrong).

Your error isn't where you expect it to be. Let's take a look at the
string you're actually passing:

>>> sql_tablename_orig = 'pyfilestat_drive_e_2010_01_27_16_48_31'
>>> sql_command_stat = 'SELECT COUNT(lastmoddate) FROM '+sql_tablename_orig+'WHERE lastmoddate < %d'
>>> sql_command_stat
'SELECT COUNT(lastmoddate) FROM
pyfilestat_drive_e_2010_01_27_16_48_31WHERE lastmoddate < %d'

well, hello. we seem to be having a missing space, right in between
the table name and 'WHERE.'

Also, you should use the third form, using the parameters argument of
execute(). it's the only secure one.

Hugo


More information about the Tutor mailing list