Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 2, 2019 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/545dbb663008cbb386a6e1aff88acdee to your computer and use it in GitHub Desktop.
Save parzibyte/545dbb663008cbb386a6e1aff88acdee to your computer and use it in GitHub Desktop.
import pymysql
try:
conexion = pymysql.connect(host='localhost',
user='root',
password='',
db='peliculas')
try:
with conexion.cursor() as cursor:
consulta = "INSERT INTO peliculas(titulo, anio) VALUES (%s, %s);"
#Podemos llamar muchas veces a .execute con datos distintos
cursor.execute(consulta, ("Volver al futuro 1", 1985))
cursor.execute(consulta, ("Ready Player One", 2018))
cursor.execute(consulta, ("It", 2017))
cursor.execute(consulta, ("Pulp Fiction", 1994))
conexion.commit()
finally:
conexion.close()
except (pymysql.err.OperationalError, pymysql.err.InternalError) as e:
print("Ocurrió un error al conectar: ", e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment