How to determine a variable's datatype?

Greg Jorgensen gregj at pobox.com
Sat Mar 18 20:47:10 EST 2000


"Peter Bittner" <bittneph at aston.ac.uk> wrote in message
news:38CFE3E2.8EE2CEB at aston.ac.uk...
> Is it possible in Python to determine the datatype of an object?
...
> This would be crucial for this function that I want to write in a module
> called 'sql':
...
> For a number the value added must be plain, for a string the 'value'
> must be enclosed in '-characters.
> Hence this is crucial to this application!

Are you sure you have to worry about this? The database engine will know the
expected type by the column name and will attempt a conversion. Check the
documentation for your database engine. Or just try it yourself.

(assuming col is defined as an integer):

insert table (col) values (1)
insert table (col) values ('2')

If that works (and it probably will) just quote everything and let the SQL
parser sort it out.

Greg Jorgensen
gregj at pobox.com







More information about the Python-list mailing list