首页 > 数据库技术 > 详细

python操作数据库 封装类

时间:2018-04-18 14:05:50      阅读:189      评论:0      收藏:0      [点我收藏+]
# -*- 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操作数据库 封装类

原文:https://www.cnblogs.com/hyn934/p/8874245.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!