• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

pymysql无法打开数据库

使用pymysql模块操作数据库,封装之后运行发现无法打开数据库,封装代码如下:#coding=utf-8import pymysqlclass 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-8import MysqlHelpername=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)谢谢谢谢!

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!