Column types with DB API
gh at ghaering.de
Mon Jun 29 00:21:22 CEST 2009
Thomas Robitaille wrote:
> I'm trying to use DB API compliant database modules
> (psycopg2,MySQLdb,SQLite) to access SQL databases, and I am trying to
> determine the type of each column in a table. The DB API defines
> cursor.description which contains information about the column names and
> types (once .execute() has been used to select part or all of the table),
> but the types are expressed as integers. I realize that these numbers can be
> checked against the type objects such as MySQLdb.NUMBER to determine whether
> a given column is a number, a date, text, etc. However, is there any way to
> determine what the sub-type of a column is, for example if a column is a
> NUMBER, is there a way to determine if it is a 2, 4, or 8-byte integer or
> real number?
Not with the DB-API. You'll have to implement all this for each database
separately. For some databases (like SQLite) it's even impossible.
I suggest you don't bother with cursor.description at all. Depending on
it doesn't harmonize with the way Python is typically used: duck typing.
More information about the Python-list