Created
February 10, 2024 18:14
-
-
Save ronakjain2012/722b41f85cd7990bc122ca77e744a368 to your computer and use it in GitHub Desktop.
MySQL and Python the easy way single script
This file contains 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
# 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