python AttributeError: ‘MysqlDb‘ object has no attribute ‘cur‘

created at 06-19-2022 views: 8

reason

The code has been reporting errors and has been unable to execute. I have read the code for a long time, and I have read it for a long time according to the information reported, but I have not seen any problems.

Traceback (most recent call last):
  File "C:\Users\Administrator\PycharmProjects\pythonProject\util\db_util.py", line 16, in <module>
    r = mydb.query("select * from `case`")
  File "C:\Users\Administrator\PycharmProjects\pythonProject\util\db_util.py", line 10, in query
    self.cur.execute(sql)
AttributeError: 'MysqlDb' object has no attribute 'cur'
import pymysql

class MysqlDb:

    def __int__(self):
        self.conn = pymysql.connect(host="192.168.253.128", user="root", password="123456", database="testcase")
        self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

    def query(self,sql):
        self.cur.execute(sql)
        data = self.cur.fetchall()
        return data

if __name__ == '__main__':
    mydb = MysqlDb()
    r = mydb.query("select * from `case`")
    #r = mydb.execute("insert into `case` (`app`) values('xd')")
    print(r)

solution

Through code comparison, it is found that when __init__ is called, __int__ is brought out inexplicably. What a shame, I am really speechless. The key is that the error message is wrong!

summary

Auto-completion is still to be seen clearly, the information given by the python error report may not be accurate.

created at:06-19-2022
edited at: 06-19-2022: