1.连接数据库import pymysql # 引入pymysql模块class MysqlUtil(): def __init__(self): # 初始化方法,连接数据库 网页链接 = pymysql.connect(host='localhost', user='root', password='123456', db='news') # 建立连接 网页链接 = self.db.cursor(cursor=pymysql.cursors.DictCursor) # 设置 游标,并将游标设置为字典类型 # 插入数据库 sql:插入数据库的sql语句 def insert(self, sql): try: count = self.cursor.execute(sql) # 执行sql语句 self.db.commit() # 提交到数据库执行 return count except Exception: # 捕获所有异常 print("发生异常", Exception) self.db.rollback() # 如果发生异常,则回滚 finally: self.db.close() # 关闭数据库连接 # 查询数据库:单个结果集 fetchone(): 该方法获取下一个查询结果集。结果集是一 个对象 def fetchone(self, sql): try: self.cursor.execute(sql) # 执行sql语句 result = self.cursor.fetchone() except Exception as e: print('发生异常:', e) # 输出异常信息 self.db.rollback() finally: self.db.close() return result # 查询数据库:多个结果集 def fetchall(self, sql): try: self.cursor.execute(sql) results = self.cursor.fetchall() except Exception as e: print('发生异常:', e) self.db.rollback() finally: self.db.close() return results def delete(self, sql): try: count = self.cursor.execute(sql) self.db.commit() return count except Exception as e: print('发生异常:', e) self.db.rollback() finally: self.db.close() def update(self, sql): try: count = self.cursor.execute(sql) self.db.commit() return count except Exception as e: print('发生异常:', e) self.db.rollback() finally: self.db.close()2.具体代码 from flask import *from mysql_util import *app=Flask(__name__)@app.route('/')def show(): return r