pymysql无法打开数据库

使用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)

谢谢谢谢!


慕圣7575996
浏览 1234回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MySQL
Python