Created
September 28, 2018 12:24
-
-
Save saiprasad1996/b40bedfa97d5d9a54281ba0d006ae0f3 to your computer and use it in GitHub Desktop.
Python connectivity with mysql
This file contains hidden or 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
import pymysql | |
import pymysql.cursors | |
class Login: | |
def __init__(self,**kwargs): | |
self.username = kwargs['username'] | |
self.password = kwargs['password'] | |
self.connection = None | |
self.connect() | |
def connect(self): | |
self.connection = pymysql.connect( | |
host='127.0.0.1', | |
user='root', | |
password='', | |
db='alumni', | |
cursorclass = pymysql.cursors.DictCursor | |
) | |
if self.connection is not None: | |
print(self.connection) | |
print("Connection successful") | |
def login(self): | |
query = 'SELECT * FROM login where username="{}" and password= "{}"'.format(self.username,self.password) | |
result = None | |
try: | |
with self.connection.cursor() as cursor: | |
# Read a single record | |
cursor.execute(query) | |
result = cursor.fetchall() | |
print(result) | |
if len(result) == 1: | |
print("Login successful") | |
else : | |
print("Login failed") | |
except pymysql.err.ProgrammingError: | |
print("Table does not exists") | |
finally: | |
self.connection.close() | |
l = Login(username="saiprasad",password="password") | |
l.connect() | |
l.login() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment