# Subclassing mysql.connector

**URL:** <https://discuss.python.org/t/subclassing-mysql-connector/109202>\
**Category:** Python Help\
**Created:** [September 23, 2026, 4:30pm UTC](https://discuss.python.org/t/subclassing-mysql-connector/109202 "2026-09-23T16:30:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [September 23, 2026, 4:30pm UTC](https://discuss.python.org/t/subclassing-mysql-connector/109202/1 "2026-09-23T16:30:52Z")

</div>

Any idea why I can’t subclass mysql.connector like this:

```python
import mysql.connector

class Database(mysql.connector.MySQLConnection):
    def __init__ (self, *args, **kwargs):
        super(). __init__ (**kwargs)

db_config={'host':'...', 'database':'...', 'user':'...', 'passwd':'...', 'autocommit':True}
db = Database(**db_config)
db_conn = db.connect(**db_config)
print(db_conn)
oCursor = db_conn.cursor() # <- this fails with 'NoneType' object has no attribute 'cursor'

oCursor.execute("SELECT 1")
db_conn.close()

```

---

<div class="post-metadata">

**Author:** ![duncathan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/duncathan/32/25521_2.png) [@duncathan](https://discuss.python.org/u/duncathan)\
**Post date:** [September 23, 2026, 5:03pm UTC](https://discuss.python.org/t/subclassing-mysql-connector/109202/2 "2026-09-23T17:03:38Z")

</div>

> **[MySQL :: MySQL Connector/Python Developer Guide :: 10.2.5...](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-connect.html)**

If this is the documentation for the library you’re using, it looks like the `connect()` function doesn’t return a value. Does this work?

```py
db = Database(**db_config)
db.connect(**db_config)
oCursor = db.cursor()

```

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [September 23, 2026, 10:25pm UTC](https://discuss.python.org/t/subclassing-mysql-connector/109202/3 "2026-09-23T22:25:32Z")

</div>

Still get `AttributeError: 'NoneType' object has no attribute 'cursor'`.

---

<div class="post-metadata">

**Author:** ![duncathan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/duncathan/32/25521_2.png) [@duncathan](https://discuss.python.org/u/duncathan)\
**Post date:** [September 23, 2026, 10:35pm UTC](https://discuss.python.org/t/subclassing-mysql-connector/109202/4 "2026-09-23T22:35:36Z")

</div>

Really? Can you show the updated code and which line is failing?

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [September 23, 2026, 10:49pm UTC](https://discuss.python.org/t/subclassing-mysql-connector/109202/5 "2026-09-23T22:49:11Z")

</div>

```python
import mysql.connector
class Database(mysql.connector.MySQLConnection):
        def __init__ (self, *args, **kwargs):
            super(). __init__ (**kwargs)

db_config={'host':'...', 'database':'...', 'user':'...', 'passwd':'...'}

db = Database(**db_config)
db.connect(**db_config)
oCursor = db.cursor() # <- this fails with 'NoneType' object has no attribute 'cursor'

```

---

<div class="post-metadata">

**Author:** ![jonathandung](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonathandung/32/35459_2.png) [@jonathandung](https://discuss.python.org/u/jonathandung)\
**Post date:** [September 24, 2026, 12:54am UTC](https://discuss.python.org/t/subclassing-mysql-connector/109202/6 "2026-09-24T00:54:28Z")

</div>

According to the source, instantiating the superclass with keyword arguments already creates the connection, so there is no need to call `db.connect` afterwards. Since the call to `connect` could not have modified the global to set `db` to `None`, some attribute of the `db` object must have been set to `None` instead to cause the error. Can you paste the traceback here?

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [September 24, 2026, 4:12pm UTC](https://discuss.python.org/t/subclassing-mysql-connector/109202/7 "2026-09-24T16:12:15Z")

</div>

Well I’ll be …

It is now working correctly but I don’t know why. Yesterday I copied that code back and forth to check it was correct and it always fell over with the `NoneType` error, however today all is working fine. I wonder if I did something within PyCharm to update `mysql.connector.python` or something like that.

Either way many thanks for the help @duncathan and @jonathandung, latest working code below:

```python
import mysql.connector
class Database(mysql.connector.MySQLConnection):
    def __init__ (self, *args, **kwargs):
        super(). __init__ (**kwargs)
    def runSQL(self, sql):
        print(self.is_connected())
        with self.cursor() as oCursor:
            oCursor.execute(sql)
            data = oCursor.fetchall()
            return data

db_config={'host':'...', 'database':'...', 'user':'...', 'passwd':'...'}

db = Database(**db_config)
print(db.runSQL("SELECT 1"))
db.close()

```
