Skip to content

Instantly share code, notes, and snippets.

@saiprasad1996
Created September 28, 2018 12:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saiprasad1996/b40bedfa97d5d9a54281ba0d006ae0f3 to your computer and use it in GitHub Desktop.
Save saiprasad1996/b40bedfa97d5d9a54281ba0d006ae0f3 to your computer and use it in GitHub Desktop.
Python connectivity with mysql
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