下面将python操作数据库进行了封装
from pymysql import connect
class JD(object):
def init(self):
self.conn = connect(…)
self.cursor = self.conn.cursor()
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42def __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封装内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复