python y sqlite3
Chema Cortes
pych3m4 en gmail.com
Sab Oct 21 18:46:00 CEST 2006
El 2006/10/21, Peon Blanco <peonblanco83 en gmail.com> escribió:
> hola espero y me puedan ayudar soy principiante tanto en python como en
> sql
>
> from sqlite3
>
> conn = sqlite3.connect ("bandas.db")
> cursor = conn.cursor ()
>
> SQL = """INSERT INTO BANDAS (NOMBRE, P_ORIGEN, GENERO)
> VALUES ('EL RECODO', 'MEXICO','BANDA')"""
> cursor.execute(SQL)
> conn.commit()
>
> SQL = "select NOMBRE, P_ORIGEN, GENERO from BANDAS order by NOMBRE"
> cursor.execute(SQL)
> row = cursor.fetchone()
> plantilla = " %30s %15s %30s \n "
> print plantilla % ('NOMBRE', 'P_ORIGEN', 'GENERO')
>
> while row != None :
> print plantilla % ( row ['NOMBRE'],
> row ['P_ORIGEN'],
> row ['GENERO']);
> row = cursor.fetchone()
> conn.close
>
>
> IDLE 1.1.3
> >>> ================================ RESTART
> ================================
> >>>
> NOMBRE P_ORIGEN
> GENERO
>
>
> Traceback (most recent call last):
> File "C:\Documents and Settings\PEON\Escritorio\SQLite\consqlite.py", line
> 28, in -toplevel-
> print plantilla % ( row ['NOMBRE'],
> TypeError: tuple indices must be integers
> >>>
El cursor.fetchone devuelve una tupla, no un diccionario. Simplemente
cambia esta línea:
print plantilla % ( row ['NOMBRE'],
row ['P_ORIGEN'],
row ['GENERO']);
por ésta
print plantilla % row
...y en la línea siguiente, te falta los paréntesis de llamada:
conn.close()
Más información sobre la lista de distribución Python-es