# 1.导入模块 import pymysql class MysqlHelper(object): def __init__(self) -> None: self.con = None self.cur = None try: # 2.建立连接 self.con = pymysql.Connect(user="root", password="123456", database="goods") # 3.创建游标 self.cur = self.con.cursor() except Exception as e: print(e) # 查询一条 def one(self, query, args=None): try: self.cur.execute(query, args) return self.cur.fetchone() except Exception as e: print(e) finally: self._close() # 查询n条 def many(self, query, args=None): try: self.cur.execute(query, args) n = int(input("请输入查询数据条数")) return self.cur.fetchmany(size=n) except Exception as e: print(e) finally: self._close() #
MySQL数据库04--python操作数据库
最新推荐文章于 2022-08-15 15:36:03 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)