Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: code for connecting MySQL with Python for Data Extraction #872

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Python/SQL_Connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Contributer : Ravin D (ravin-d-27)
# This is the code for connecting MySQL With Python for Data Extraction

import mysql.connector as m

class MySQL_Connector(object):

def __init__(self, username, password, host, db_name): # Essential things to connect MySQL and Python interface

self.mydb = m.connect(host = host, user = username, passwd = password, database = db_name)
self.c = self.mydb.cursor()

def fetch_all(self, table): # This is the method to fetch all the data from the table of given database

try:
query = f"select * from {table}"
self.c.execute(query)
b = self.c.fetchall()
return b
except:
print("Unable to fetch the data because of invalid parameters")

def select_data(self, tablename, *args): # This is the method to fetch the particular data from the table of given database

string = ''

for i in args:
string+=i+","

try:
string = string[:-1]
self.execute(f"select {string} from {tablename}")
b = self.c.fetchall()
return b
except:
print("Unable to fetch the data because of invalid parameters")