I am trying to use MySQL Connector/Python to work with a database. Connection is establishing succesfully, e.g. I was able to insert new data to the table. And SELECT queries are working just fine with normal integer or char values.
But when I try to execute SELECT query with one of the columns being int(small, medium, big, doesn't matter) I meet ValueError: invalid literal for int() with base 0: '00002'
How to repeat:
Create a table where one of the columns of type
int unsigned zerofill
(or any other integer type withzerofill
). For example, doCREATE TABLE test_table (id INT UNSIGNED ZEROFILL);
and thenINSERT INTO test_table VALUES (1), (2), (3);
Try to do cursor.execute(query) with SELECT query that includes zerofill field, e.g.
SELECT * FROM test_table;
What should happen: program outputs
ValueError: invalid literal for int() with base 0: '0000000001'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\user\Documents\testSQL.py", line 25, in <module>
cursor.execute(query)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\cursor_cext.py", line 272, in execute
self._handle_result(result)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\cursor_cext.py", line 163, in _handle_result
self._handle_resultset()
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\cursor_cext.py", line 651, in _handle_resultset
self._rows = self._cnx.get_rows()[0]
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\connection_cext.py", line 273, in get_rows
row = self._cmysql.fetch_row()
SystemError: <built-in method fetch_row of _mysql_connector.MySQL object at 0x000002555DDBC350> returned a result with an error set
It looks like mysql-connector is trying to do int(some_number, base=0)
when converts database integer to python int() value. But it actually is a number with base 10. What can I do to avoid such an error?