[Python-es] consulta SQL
Antonio Beamud Montero
antonio.beamud en gmail.com
Vie Jun 18 12:47:26 CEST 2010
monoBOT escribió:
> Hola ... como puedo obtener la cantidad de columnas de una tabla?
>
> hice lo siguiente de modo rudimentario pero seguramente existe una
> forma mucho mas directa ... alguna pista?
>
>
> orden = 'SELECT * FROM nombretabla WHERE id = 1'
> cursor.execute(orden)
>
> for objeto in cursor:
> valor = len(objeto)-1
> ------------------------------------------------------------------------
>
>
de http://www.python.org/dev/peps/pep-0249/:
Cursor Objects should respond to the following methods and
attributes:
.description
This read-only attribute is a sequence of 7-item
sequences.
Each of these sequences contains information describing
one result column:
(name,
type_code,
display_size,
internal_size,
precision,
scale,
null_ok)
The first two items (name and type_code) are mandatory,
the other five are optional and are set to None if no
meaningful values can be provided.
This attribute will be None for operations that
do not return rows or if the cursor has not had an
operation invoked via the .execute*() method yet.
The type_code can be interpreted by comparing it to the
Es decir, si haces len(cursor.description) te dice el total de columnas.
Más información sobre la lista de distribución Python-es