python 连接mysql代码:import pymysqlclass lhd_mysql(object):def __init__(self, ip, port, user, password, databasename):self.ip = ipself.port = portself.user = userself.password = passwordself.databasename = databasenamedef connect_mysql(self):try:db = pymysql.connect(self.ip, self.user, self.password, self.databasename, self.port)return dbexcept Exceptionas e:print(e)def action_sql(self, db):# 创建游标,游标是用来执行sql语句的 cursor = db.cursor()sql ="show tables" cursor.execute(sql)# execute 执行的意思 res = cursor.fetchall()print(res)print(res[0][0])db.commit()# commit 提交的意思 cursor.close()db.close()lhd = lhd_mysql('127.0.0.1',3306,'root','123456','students')# 这里传递的是数据库的名字,相当于客户端那里使用的use students;db = lhd.connect_mysql()lhd.action_sql(db)=================================================================mysql修改密码代码:格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:mysql> set password for root@localhost = password('123'); =================================================================