import pymysql
# mysql class
class MySqlUtil(object):
# 连接数据库,初始化游标
def __init__(self):
self.conn = pymysql.connect(host=db_host,
user=db_user,
password=db_pwd,
db=db)
self.cursor = self.conn.cursor()
# 从数据库中获取数据
def get_data_from_db(self, sql):
try:
self.cursor.execute(sql)
results = self.cursor.fetchall()
return results
except pymysql.Error as e:
print('Mysql get_data_from_db Error {0}:'.format(e))
# 数据库执行插入、删除、创建表、删除表、、、
def exec_db(self, sql):
try:
cur = self.conn.cursor()
cur.execute(sql)
self.conn.commit()
except pymysql.Error as e:
print('Mysql exec_db Error {}'.format(e))
# 析构函数实现数据库关闭
def __del__(self):
self.cursor.close()
python 连接mysql封装
最新推荐文章于 2024-08-13 17:47:42 发布
该博客主要围绕Python连接MySQL并进行封装展开,虽未给出具体内容,但可知核心是利用Python实现与MySQL数据库的连接及封装操作,属于后端开发中数据库交互的范畴。
846

被折叠的 条评论
为什么被折叠?



