mysql.connector method chaining error (ReferenceError: weakly-referenced object no longer exists)

Viewed 58
import mysql.connector
mysql.connector.connect(host="localhost",user='xxxx',password='xxxx',database="xxxx").cursor().execute("show tables")

Error :

Traceback (most recent call last): File "xx:\xx\xx.py", line 10, in mysql.connector.connect(host="localhost",user='xxxx',password='xxxx',database="xxxx").cursor().execute("show tables") File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 536, in execute if not self._connection: ReferenceError: weakly-referenced object no longer exists

I know if I write it this way there is no error :

import mysql.connector
database=mysql.connector.connect(host="localhost", user='xxxx', password='xxxx',database="xxxx")
cursor=database.cursor()
cursor.execute("show tables")

Why can't I chain my code? Why is it like this? Any help much appreciated <3!

0 Answers
Related