#-*-Coding:utf-8-*-
# Author:hy
# @Time: 2018/2/1610:24
Import Pymysql
# Encapsulation Class
class Mysqlhelp (object):
# Construction
def __init__ (self, host, user, passwd, DB, port=3306):
self.host = Host
self.user = user
Self.port = Port
self.passwd = passwd
self.db = db
# Create a connection
def open_coon (self):
Self.coon = Pymysql.connect (Host=self.host, Port=self.port, User=self.user, PASSWD=SELF.PASSWD, db=self.db)
self.cursor = self.coon.cursor ()
# Close Connection
def close (self):
self.cursor.close ()
self.coon.cursor ()
# Call Statement
def insert_delete_update (self, SQL, params):
Try:
Self.open_coon ()
self.cursor.execute (SQL, params=[])
print ("OK")
Self.coon.commit ()
self.close ()
except Exception as Erorr:
print (Erorr)
# The query receives all of the returned result rows
def select_fetchall (self, SQL, params=[]):
Try:
Self.open_coon ()
self.cursor.execute (SQL, params)
results = Self.cursor.fetchall ()
Self.coon.commit ()
self.close ()
return Results
except Exception as Erorr:
print (Erorr)
Python Operations Database Encapsulation class