pycharm 操作mysql
import pymysql class Mysql_Python: def __init__(self,host,port,user,password,database): self.host = host self.port = port self.user = user self.password = password self.database = database def coonect(self): db = pymysql.connect(host=self.host,port=self.port,user=self.user,password=self.password,database=self.database) cursor = db.cursor() return db,cursor def select(self,cursor,sql_str): try: cursor.execute(sql_str) # 执行SQL语句 results = cursor.fetchall() # 获取所有记录列表 return results except: print("Error: unable to fetch data") print("失败") def insert(self,db,cursor,sql_str): try: cursor.execute(sql_str) # 执行sql语句 db.commit() # 提交到数据库执行 print("成功") except: db.rollback() # 如果发生错误则回滚 print("失败") def delete(self,db,cursor,sql_str): try: cursor.execute(sql_str) # 执行SQL语句 db.commit() # 提交修改 print("成功") except: db.rollback() # 发生错误时回滚# 关闭连接 print("失败") def updata(self,db,cursor,sql_str): try: cursor.execute(sql_str) # 执行SQL语句 db.commit() # 提交修改 print("成功") except: db.rollback() # 发生错误时回滚# 关闭连接 print("失败") def close(self,db,cursor): cursor.close() db.close()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?