为了账号安全,请及时绑定邮箱和手机立即绑定

pymysql无法打开数据库

pymysql无法打开数据库

慕圣7575996 2018-03-06 19:12:07
使用pymysql模块操作数据库,封装之后运行发现无法打开数据库,封装代码如下: #coding=utf-8 import pymysql class MysqlHelper:     def __init__(self,host,port,db,user,passwd,charset):         self.host=host         self.port=port         self.db=db         self.user=user         self.passwd=passwd         self.charset=charset     def open(self):         try:             self.conn=pymysql.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset)             self.cursor=self.conn.cursor()         except Exception as e:             print("打开失败")     def close(self):         try:             self.cursor.close()             self.conn.close()         except Exception as e:             print("关闭失败")     def cud(self,sql,params):         try:             self.open()             self.cursor.execute(sql,params)             self.conn.commit()             self.close()             print("ok")         except Exception as e:             print("错误1")     def all(self,sql,params):         try:             self.open()             self.cursor.execute(sql, params)             result=self.cursor.fetchall()             self.close()             return result         except Exception as e:             print("错误2")  测试代码如下:  #coding=utf-8 import MysqlHelper name=input("name:") id1=input("id:") sql="üpdate student set name=%s where id=%s" params=[name,id1] sqlhelper=MysqlHelper.MysqlHelper("localhost",3306,"CR","root","122413","utf-8") sqlhelper.cud(sql,params) 谢谢谢谢!
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 1228 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信