uPyMySQL

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
dvrhax
Posts: 14
Joined: Fri Jul 07, 2017 11:13 pm

uPyMySQL

Post by dvrhax » Fri Jul 07, 2017 11:39 pm

Currently porting the PyMySQL pure-python implementation mysql client for use with micropython. Its a pretty ugly hack but I'm making progress. Right now though I'm stuck on a portion of code using super. I know from reading through the forums that the micropython implementation of super isn't that robust and was wondering if anyone has any experience with writing work arounds for the use of super and if they have some suggestions. If anyone would like to check out the project you can find it here: https://github.com/dvrhax/uPyMySQL I'm hoping to use this for a data-logger for a couple of projects I'm doing.

Thanks

dvrhax
Posts: 14
Joined: Fri Jul 07, 2017 11:13 pm

Re: uPyMySQL

Post by dvrhax » Sat Jul 08, 2017 4:50 am

So I finally deciphered what the super call was doing and was able to rewrite the classes using traditional inheritance with some duplication of code. My small test case is now working. Hopefully someone will find it useful beside myself. Still some limited functionality especially on the type conversion side as micropython has only limited support for things like datetime.

User avatar
pythoncoder
Posts: 3241
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uPyMySQL

Post by pythoncoder » Sat Jul 08, 2017 7:01 am

Are you experiencing actual problems with super()? I had trouble in the past with it occasionally returning `None` but I haven't seen that for some time. You can of course replace it with a specific superclass name:

Code: Select all

class Foo():
    def __init__(self):
        pass

class Bar(Foo):
	def __init__(self):
	    Foo.__init__(self)  # replaces super().__init__()
Peter Hinch

dvrhax
Posts: 14
Joined: Fri Jul 07, 2017 11:13 pm

Re: uPyMySQL

Post by dvrhax » Sat Jul 08, 2017 2:16 pm

I was getting the following error:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 16, in <module>
  File "test.py", line 14, in <module>
  File "pymysql/cursors.py", line 169, in execute
  File "pymysql/cursors.py", line 326, in _query
  File "pymysql/cursors.py", line 382, in _do_get_result
AttributeError: 'super' object has no attribute '_do_get_result'
For this code snippet:

Code: Select all

class DictCursorMixin(object):
    # You can override this to use OrderedDict or other dict-like types.
    dict_type = dict

    def _do_get_result(self):
        super(DictCursorMixin, self)._do_get_result()
        fields = []
        if self.description:
            for f in self._result.fields:
                name = f.name
                if name in fields:
                    name = f.table_name + '.' + name
                fields.append(name)
            self._fields = fields

        if fields and self._rows:
            self._rows = [self._conv_row(r) for r in self._rows]

    def _conv_row(self, row):
        if row is None:
            return None
        return self.dict_type(zip(self._fields, row))


class DictCursor(DictCursorMixin, Cursor):
    """A cursor which returns results as a dictionary"""
I ended up rewriting it and just bringing all the code from DictCursorMixin and the code from the _do_get_result method from the cursor class into DictCursor and remove the super call all together. I couldn't call the base class directly because DictCursorMixin is also referenced from another class in the same way, so I had to rewrite that as well.

User avatar
pythoncoder
Posts: 3241
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uPyMySQL

Post by pythoncoder » Sun Jul 09, 2017 5:21 am

OK. I've only used the zero-argument form of super() in MicroPython. Support for multiple inheritance may be incomplete - you might like to search GitHub issues. If you can produce a simple test case where MicroPython differs from CPython you could raise an issue.
Peter Hinch

dvrhax
Posts: 14
Joined: Fri Jul 07, 2017 11:13 pm

Re: uPyMySQL

Post by dvrhax » Sun Jul 09, 2017 6:11 am

Posted it to this issue here: https://github.com/micropython/micropython/issues/3205

The only thing I could find that was close was the issue you had posted back in May of 2016 but I wasn't convinced that it was the same issue. I did supply a test case which exhibits the bug however details beyond that are pretty sparse as I'm not sure I understand the issue real well myself.

Thanks for your help

User avatar
rdagger
Posts: 87
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: uPyMySQL

Post by rdagger » Mon Jun 18, 2018 6:30 pm

I tried your library and I'm getting the following error when I run the demo code:
[Errno 104] ECONNRESET
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "sqltest.py", line 9, in <module>
File "upymysql/__init__.py", line 86, in Connect
File "upymysql/connections.py", line 658, in __init__
File "upymysql/connections.py", line 923, in connect
File "upymysql/connections.py", line 923, in connect
File "upymysql/connections.py", line 875, in connect
File "upymysql/connections.py", line 873, in connect
AttributeError: 'OSError' object has no attribute 'errno'
Using ESP32 running LoBo build. Trying to insert data to MariaDB 10 database running on a local NAS.

dvrhax
Posts: 14
Joined: Fri Jul 07, 2017 11:13 pm

Re: uPyMySQL

Post by dvrhax » Tue Feb 26, 2019 11:25 pm

Did you have any luck resolving this? I'm getting back into the codebase as I can't get it to fit onto my Feather Huzzah and I'd like to incorporate whatever fixes you did or work on fixing it myself. Do you have the code that you ran to get the error?

User avatar
rdagger
Posts: 87
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: uPyMySQL

Post by rdagger » Wed Feb 27, 2019 7:37 pm

dvrhax wrote:
Tue Feb 26, 2019 11:25 pm
Did you have any luck resolving this? I'm getting back into the codebase as I can't get it to fit onto my Feather Huzzah and I'd like to incorporate whatever fixes you did or work on fixing it myself. Do you have the code that you ran to get the error?
I was able to get a simple insert query working, but I shelved the project at that point.

dvrhax
Posts: 14
Joined: Fri Jul 07, 2017 11:13 pm

Re: uPyMySQL

Post by dvrhax » Sat Mar 02, 2019 2:44 am

I've only recently picked this back up and I'm really only using it for logging functionality so basic inserts myself. I have gone back and shrunk the size considerably and also worked through some more kinks including some with regards to "errno" so If you do pick up the project again make sure to use the updated code.

Good luck

Post Reply