运行如下python脚本时,报错AttributeError: 'Mysql_op' object has no attribute 'cursor',怎么也排查不出错误,在此请教有遇到类似问题的朋友,告知解决办法,谢谢。报错信息:’File "/root/PycharmProjects/Exam/libs/mysql_op.py", line 40, in
db.select('select * from emp;')
File "/root/PycharmProjects/Exam/libs/mysql_op.py", line 18, in select
self.cursor.execute(sql_text)
AttributeError: 'Mysql_op' object has no attribute 'cursor'
import pymysql
class Mysql_op:
def __int__(self):
self.db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123', database='db_01', charset='utf8')
self.cursor = self.db.cursor()
# 查询方法
def select(self, sql_text, many=True):
self.cursor.execute(sql_text)
if many:
result = self.cursor.fetchall()
else:
result = self.cursor.fetchone()
return result
# 定义曾删改的公共操作方法
def do(self, sql_text):
try:
self.cursor.execute(sql_text)
except Exception as e:
self.db.rollback()
raise e
self.db.commit()
self.cursor.close()
def exit(self):
# self.cursor.close()
self.db.close()
if __name__ == '__main__':
db = Mysql_op()
db.select('select * from emp;')
用Navicat连一下试试能连上这个库不?看代码没啥问题,应该是连接不到db