Numbers in SQL statements

claudius at claudius at
Sat Nov 6 05:15:59 CET 1999

Nick Collier <nick at> says:
>I've been creating SQL statements from dictionaries holding the field
>name and the new value to insert or update. As might be expected this
>new value is often a number. When turning this number into a string in
>order to create the sql statement the number will have a "L" appended if
>it is a long. Consequently, the resulting SQL might look like the
>update myTable
>set id_num = 23L
>where food = 'spam'
>I've been testing if the value is a number whose string representation
>ends with "L" and then stripping the "L", but am pretty sure there must
>be a better way. If so, any suggestions as to what is?

Try (if you're using a module that supports the full Python DB API 1.0):

cursor.execute('update myTable set id_num = ? where food = ?', (id_num, food))

This mechanism is much more forgiving than stringifying directly into the

I've been using this format with great success (including storing binary
pickled datastructures and marshalled Python bytecode in the database.  Whee!)

More information about the Python-list mailing list