Skip to content

Instantly share code, notes, and snippets.

@scionoftech
Last active April 24, 2021 08:47
Show Gist options
  • Save scionoftech/20f213f480e2116ab764230d14864c7f to your computer and use it in GitHub Desktop.
Save scionoftech/20f213f480e2116ab764230d14864c7f to your computer and use it in GitHub Desktop.
import mysql.connector
from mysql.connector import errorcode
def mysql_connection(f):
def with_connection_(*args, **kwargs):
conn = mysql.connector.connect(
host=host,
user=user,
passwd=password,
database=database
)
func = None
try:
func = f(conn, *args, **kwargs)
return func
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
conn.rollback()
finally:
conn.commit()
conn.close()
return func
return with_connection_
@mysql_connection
def updatefiles(conn,query):
mycursor = conn.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment