我是靠谱客的博主 伶俐小鸽子,这篇文章主要介绍python操作MySQL封装,现在分享给大家,希望可以做个参考。

下面将python操作数据库进行了封装
from pymysql import connect

class JD(object):
def init(self):
self.conn = connect(…)
self.cursor = self.conn.cursor()

def __del__(self):
self.cursor.close()
self.conn.close()
def show_all_info(self):
sql = "..."
self.execult_sql(sql)
def show_cate_info(self):
sql="..."
self.execult_sql(sql)
def show_brand_info(self):
sql="..."
self.execult_sql(sql)
def insert_info(self):
info = input("请输入一条数据")
sql="..."%info
self.cursor.execute(sql)
self.conn.commit()
def execult_sql(self, sql):
self.cursor.execute(sql)
for temp in self.cursor.fetchall():
print(temp)
@staticmethod
def print_menu():
print("1----查询所有数据")
print("2----查询分类")
print("3----查询品牌")
print("4----插入一条数据")
return input("请输入功能数字:")
def run(self):
while True:
num = self.print_menu()
if num == "1":
self.show_all_info()
elif num == "2":
self.show_cate_info()
elif num == "3":
self.show_brand_info()
elif num == "4":
self.insert_info()
else:
print("输入错误")

def main():
jd = JD()
jd.run()

if name == “main”:
main()

此处实现了数据库查询和插入的封装操作。

最后

以上就是伶俐小鸽子最近收集整理的关于python操作MySQL封装的全部内容,更多相关python操作MySQL封装内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部