码迷,mamicode.com
首页 > 数据库 > 详细

python操作数据库 封装类

时间:2018-04-18 14:23:56      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:host   int   ret   help   delete   size   cursor   param   ams   

# -*- 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操作数据库 封装类

标签:host   int   ret   help   delete   size   cursor   param   ams   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!