Created
June 4, 2015 11:52
-
-
Save psicobyte/de5d0fa0c91f2ca6d8a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/python | |
# -*- coding: utf-8 -*- | |
import MySQLdb | |
# Establecemos la conexión | |
Conexion = MySQLdb.connect(host='localhost', user='conan',passwd='crom', db='DBdeConan') | |
# Creamos el cursor | |
micursor = Conexion.cursor() | |
# Ejecutamos un insert directamente | |
micursor.execute("INSERT INTO Victimas (id,Nombre,Profesion,Muerte) VALUES (1, \"Ejercito de Zombies\",\"Muertos Vivientes\",\"Desmembramiento a espada\");") | |
# Lo mismo, pero por medio de una variable | |
query= "INSERT INTO Victimas (id,Nombre,Profesion,Muerte) VALUES (2, \"Vampiro feo\",\"Muertos Vivientes\",\"Estaca de madera\");" | |
micursor.execute(query) | |
# Hacemos un commit, por si las moscas | |
Conexion.commit() | |
# Ahora vamos a hacer un SELECT | |
query= "SELECT * FROM Victimas WHERE id=1;" | |
micursor.execute(query) | |
# Obtenemos el resultado con fetchone | |
registro= micursor.fetchone() | |
# Imprimimos el registro resultante | |
print registro | |
# Esto que sigue es para borrar el contenido de la base de datos, | |
# y que no se nos acumule al ir haciendo pruebas | |
query= "DELETE FROM Victimas WHERE 1;" | |
micursor.execute(query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment