Python Operations Database Encapsulation class

Source: Internet
Author: User

#-*-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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.