Skip to content

Instantly share code, notes, and snippets.

@ronakjain2012
Created February 10, 2024 18:14
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 ronakjain2012/722b41f85cd7990bc122ca77e744a368 to your computer and use it in GitHub Desktop.
Save ronakjain2012/722b41f85cd7990bc122ca77e744a368 to your computer and use it in GitHub Desktop.
MySQL and Python the easy way single script
# require: pip3 install mysql-connector-python
import mysql.connector
class MySQLManager:
def __init__(self, host, user, passwd, database, port=3306):
self.mydb = mysql.connector.connect(
host=host,
user=user,
passwd=passwd,
database=database,
port=port,
connect_timeout=30
)
self.mycursor = self.mydb.cursor()
def create_table(self, table_query):
self.mycursor.execute(table_query)
def insert_data(self, insert_query, val):
self.mycursor.execute(insert_query, val)
self.mydb.commit()
def select_data(self, select_query):
self.mycursor.execute(select_query)
return self.mycursor.fetchall()
def update_data(self, update_query):
self.mycursor.execute(update_query)
self.mydb.commit()
def delete_data(self, delete_query):
self.mycursor.execute(delete_query)
self.mydb.commit()
def close_connection(self):
self.mycursor.close()
self.mydb.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment