python操作数据库 封装类
Posted hyn934
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python操作数据库 封装类相关的知识,希望对你有一定的参考价值。
# -*- coding:utf-8 -*-
# Author:Hy
# @Time :2018/2/1610:24
import pymysql
# 封装类
class MysqlHelp(object):
# 构造
def __init__(self, host, user, passwd, db, port=3306):
self.host = host
self.user = user
self.port = port
self.passwd = passwd
self.db = db
# 创建连接
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()
# 关闭连接
def close(self):
self.cursor.close()
self.coon.cursor()
# 调用语句
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)
# 查询 接收全部的返回结果行
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操作数据库 封装类的主要内容,如果未能解决你的问题,请参考以下文章
十六Python操作excel(.xlsx)封装类MyPyExce
博主推荐Python 基于XlwingsOpenpyxl自己重新封装Python操作Excel类