-
Notifications
You must be signed in to change notification settings - Fork 5
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
Error writing null values to table. #6
Comments
I think it may be an issue with your code. You're not passing a tuple, in the first instance you're passing a float and in the second instance a NoneType I suspect that the function is overloaded and can either handle an iterable or a value but may stick on the None. Try putting a comma after value in data = (value) to get data = (value, ) and see if that works. |
Thank you for the reply. Slightly modified code for clarity:
Output:
Have also tried with data=(None) and data=(None,) and it gives the same error. So it looks like the execute function accepts either a single value or a tuple but the presence of a None as either causes it to fail. |
Does it make sense to trap None and convert to 'NULL'? If that's the right answer you could do it in user code, or we could patch the library to do the conversion. |
If I execute:
Then the row with a null column is inserted correctly. The problem seems to be in the implementation of connection.cursor().execute(sql, data) Any python None values in the data tuple are not handled correctly. Perhaps they are converted to "None" in the SQL string that is ultimately executed and NOT converted to "NULL"? I can work around this by formatting an SQL string and using the connection.cursor().execute(sql) version of the call having substituted any None values with NULL. In an ideal world connection.cursor().execute(sql, data) should handle this itself (as seems to happen in the pure python version of mysqldb). It seems to be the generally accepted practice that using placeholders and a tuple of data values is preferable to guard against SQL injection so I'd like to try and use that version. In the absence of a fix it would be advisable to document this limitation. Regardless of that I'd like to thank you for doing the port from python to micropython as, as best I can tell, it's the only version available and in my case it gave a good solution. At least until I decided to change columns to allow nulls! |
I've been using this code very successfully for some months now but have just encountered a problem with writing null values into a table. I've created a simple test table and script as listed below. The table has one column called columna and it's a float datatype which permits null values and the default value is NULL. There are no indexes on the column. Writing a value of, for example 0.0, works OK but I get an error if the value is set to None.
The text was updated successfully, but these errors were encountered: