Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot import name Connection #1

Open
BryanSouza91 opened this issue Dec 9, 2019 · 19 comments
Open

cannot import name Connection #1

BryanSouza91 opened this issue Dec 9, 2019 · 19 comments

Comments

@BryanSouza91
Copy link

Hello,

thanks so much for porting this package.

I have a little program written to read an IMU and log it to a database.

ESP-32 and LSM303DLHC
MySQL running on desktop on localhost through XAMPP

I have checked all of my code three times just to be sure, here is the part of my code(line 16 in error below) that is based on your example:
connection = upymysql.connect(host='192.168.1.10', user='bryan', password=password, db='micropython_data', charset='utf8_general_ci', cursorclass=upymysql.cursors.DictCursor)

here is the error:
network config: ('192.168.1.253', '255.255.255.0', '192.168.1.1', '192.168.1.1') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 16, in <module> File "upymysql/__init__.py", line 263, in Connect ImportError: cannot import name Connection

any input would would be greatly appreciated.
Thank you, Bryan

@aadvanes
Copy link

Hello I have the same issue. I think something to do with the installation? What did I do wrong?

I execute following code on ESP8266 after copying the .\uPyMySQL\upymysql directory in the root of ESP8266.

Connect to the database

import pymysql.cursors
mariadb_connection = pymysql.connect(host='192.168.0.112',
user='aad',
password='aad',
db='test01',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)

The REPL tells me:

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
MicroPython v1.12 on 2019-12-20; ESP module with ESP8266

Type "help()" for more information.

%Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File "", line 3, in
AttributeError: 'module' object has no attribute 'connect'

@dvrhax
Copy link
Owner

dvrhax commented Apr 30, 2020

Bryan, I stripped most of the charsets out to get it small enough to fit on the ESP8266 that may be your problem. Try it with 'utf8mb4' and see if it works

Aadvanes, the example is a little outdated since I renamed the project upymsql from pymysql. Change all occurrences of pymysql to upymysql and make sure you start with:

import upymysql
import upymysql.cursors

Hopefully that will get you working

@BryanSouza91
Copy link
Author

I will give this a try in the next few days and report back! Thanks for checking this out!

@aadvanes
Copy link

aadvanes commented May 1, 2020 via email

@sasilva1998
Copy link

Hi, I believe there could be also a memory problem, I am using a ESP32, and getting the following error when trying to import connections.py files, believe this is mostly because the script is just to long and ESP32 ram can't handle it, I wish I could help breaking it in pieces but now don't have much time, either way might try soon if not corrected before that.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError: memory allocation failed, allocating 464 bytes

Great work by the way.

@aadvanes
Copy link

aadvanes commented May 2, 2020 via email

@sasilva1998
Copy link

I has happened to me before already with a ESP8266, which can't mostly import 350+ lines of code, but didn't except to have that problem with ESP32. Anyway a 1400+ lines for a script is a lot, might want to split it.

@BryanSouza91
Copy link
Author

This is the error msg I am receiving now, it is a MemoryError as well:

network config: ('192.168.1.253', '255.255.255.0', '192.168.1.1', '192.168.1.1')
Traceback (most recent call last):
  File "main.py", line 13, in <module>
  File "upymysql/__init__.py", line 88, in Connect
MemoryError: memory allocation failed, allocating 136 byte
MicroPython v1.11-580-g973f68780 on 2019-11-18; ESP32 module with ESP32

@dvrhax
Copy link
Owner

dvrhax commented May 4, 2020

I haven't tried this with an esp32 but did have working on an esp8266 compiled into the firmware. I don't think it possible to get it working without compiling it in.

@dvrhax
Copy link
Owner

dvrhax commented May 4, 2020

aadvanes Connect is a function in init.py line 85

@sasilva1998
Copy link

@dvrhax Think maybe that could be the problem, haven't tried compiling it but will, I am not completely sure, but think that way it doesn't consume that much memory, will be give feedback when tried.

@BryanSouza91
Copy link
Author

Thanks for the info @dvrhax

I will try it on the ESP32 compiled in the firmware.

@dvrhax
Copy link
Owner

dvrhax commented May 4, 2020

@sasilva1998 You may need to disable B-Tree in the build for the 8266. You can refer to here if you have any questions: https://forum.micropython.org/viewtopic.php?f=3&t=6007

@aadvanes
Copy link

aadvanes commented May 6, 2020 via email

@aadvanes
Copy link

aadvanes commented May 6, 2020 via email

@dvrhax
Copy link
Owner

dvrhax commented May 7, 2020

So that looks like an issue with micropython. That they've removed the EINTR code from uerrno. I've patched a workaround for that but you have an underlying problem. The first line shows a connection aborted error. You probably need to go through and double check all your parameters to make sure they're right. If you don't find anything there I would install https://pypi.org/project/PyMySQL/ and run from a full blown python shell. If it works we may have to trace into the code to see if we can figure out what's wrong. If it doesn't work it will likely give you a more verbose output to help track down the issue with your setup.

@cortenlabs
Copy link

cortenlabs commented Dec 20, 2022

Hello dvrhax,

I'm very interested in inserting records into a mysql-database directly from an ESP. (Now using an ESP32-C3) I can hardly believe that there seems to be so little interest in this subject. At least this seems to be the only place in the world where a serious attempt has been made to write directly into a database from a controller with Micropython, for which I thank you very much!. I am in the process of getting this up and running on an ESP32-C3. I was able to successfully co-compile the code with Micropython (which is necessary otherwise there will be memory problems). But the problem now is that the application can't get into contact with the mariadb server. This actually makes sense because I can't find anywhere how the controller connects to a WiFi network which is of course necessary. I can't find a location where Wifi-credentials are stored, or how a connection with an existing wifi connection is being made. I'm probably too much of a novice to understand what to do and maybe this is all very obvious, but at this point I have no idea how this should be done. Hopefully you would like to help me on how to link the wifi connection to your application.
Thanks in advance,
Paul

@BryanSouza91
Copy link
Author

BryanSouza91 commented Dec 20, 2022

Hello dvrhax,

I'm very interested in inserting records into a mysql-database directly from an ESP. (Now using an ESP32-C3) I can hardly believe that there seems to be so little interest in this subject. At least this seems to be the only place in the world where a serious attempt has been made to write directly into a database from a controller with Micropython, for which I thank you very much!. I am in the process of getting this up and running on an ESP32-C3. I was able to successfully co-compile the code with Micropython (which is necessary otherwise there will be memory problems). But the problem now is that the application can't get into contact with the mariadb server. This actually makes sense because I can't find anywhere how the controller connects to a WiFi network which is of course necessary. I can't find a location where Wifi-credentials are stored, or how a connection with an existing wifi connection is being made. I'm probably too much of a novice to understand what to do and maybe this is all very obvious, but at this point I have no idea how this should be done. Hopefully you would like to help me on how to link the wifi connection to your application.
Thanks in advance,
Paul

Hey Paul @cortenlabs,

I used the do_connect function(link below) in the boot file for the device so that it was connected to the network on boot. Hope that helps. Good luck.

Micropython ESP32 Networking Documentation

Bryan

@cortenlabs
Copy link

cortenlabs commented Dec 21, 2022

Hello Bryan,

Thank you very much for your reply. You helped me very well and pointed me in the right direction by telling me what is not the problem. As I already had tried something like in the link you provided, I was thinking the application could not link to the already made wifi connection. But that was not the problem! As expected I did something stupid. Starting with the default installation of mariadb I got version 10 and therefore port 3307. So I put in as hostname "192.168.0.101:3307". I know: windows thinking. The port has (of course) to be defined separately. Therefore for other novices like me, the connection phrase with port:


connection = upymysql.connect(host='localhost',
                             port = 3307,
                             user='user',
                             password='passwd',
                             db='db',
                             charset='utf8mb4',
                             cursorclass=upymysql.cursors.DictCursor)

Now it's working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants