ayuda

Cristian Domínguez cri.domo en gmail.com
Lun Oct 2 19:26:01 CEST 2006


prueba este lindo ejemplito, supongo que sabes indentar.

import sys
import MySQLdb

# connect to the MySQL server

try:
conn = MySQLdb.connect (host = "**********",
user = "***********",
passwd = "*********",
db = "*******")
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)

#---------------------------------------------------
try:
cursor = conn.cursor ()
cursor.execute ("DROP TABLE IF EXISTS animal")
cursor.execute ("""
CREATE TABLE animal
(
name CHAR(40),
category CHAR(40)
)
""")

cursor.execute ("""
INSERT INTO animal (name, category)
VALUES
('snake', 'reptile'),
('frog', 'amphibian'),
('tuna', 'fish'),
('racoon', 'mammal')
""")

print "Number of rows inserted: %d" % cursor.rowcount
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)


#---------------------------------------------------
print
cursor.execute ("SELECT name, category FROM animal")
while (1):
row = cursor.fetchone ()
if row == None:
break
print "%s, %s" % (row[0], row[1])

#---------------------------------------------------
print
cursor.execute ("SELECT name, category FROM animal")
rows = cursor.fetchall ()
for row in rows:
print "%s, %s" % (row[0], row[1])
print "Number of rows returned: %d" % cursor.rowcount

cursor.close ()
#---------------------------------------------------

print
cursor = conn.cursor (MySQLdb.cursors.DictCursor)
cursor.execute ("SELECT name, category FROM animal")
result_set = cursor.fetchall ()
for row in result_set:
print "%s, %s" % (row["name"], row["category"])

print "Number of rows returned: %d" % cursor.rowcount

#---------------------------------------------------
cursor.execute ("""
UPDATE animal SET name = 'turtle'
WHERE name = 'snake'
""")

print "Number of rows updated: %d" % cursor.rowcount
#---------------------------------------------------
cursor.execute ("""
UPDATE animal SET name = %s
WHERE name = %s
""", ("snake", "turtle"))

print "Number of rows updated: %d" % cursor.rowcount
#---------------------------------------------------

cursor.close ()
conn.commit()
conn.close ()





2006/10/2, faramir hijo de senescal de gondor <faramirt en gmail.com>:
>
> hola todo en s
>
> pido porfa q me colaboren con un codigo que tengo problema.
>
> Estoy abriendo una base de datos con MySQL, cuando introdusco los comandos
> linea por linea en el terminal, funcionan muy bien y puedo acceder al a
> informacion, pero cuando trato de hacer un programita, presenta un error
> en
> el sugando reglon.
>
> es decir
> python
> Python 2.3.4 (#1, Nov  4 2004, 14:06:56)
> [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>>  import MySQLdb
> >>> connection = MySQLdb.connect(host="-x-x-x-x-x", user="------",
> passwd="xxxxxxxxxx", db="_____" )
>
> y funciona muy bien, pero si trato de hacer lo mismo pero en un solo
> archivo
> y luego le doy atributos para ejecutarlo no funciona.
>
> Agradeciendo a tod en s por su atencion y colaboracion
>
>
>
> --
> CARLOS MARIO OSORIO
>
>
> _______________________________________________
> Python-es mailing list
> Python-es en aditel.org
> http://listas.aditel.org/listinfo/python-es
>
>
>




Más información sobre la lista de distribución Python-es