Problem: Executing a certain UPDATE query using the 'mysql-connector-python' library causes a TypeError during utf8 encoding.
Packages in my project:
colorama==0.4.6
iniconfig==2.0.0
mysql-connector-python==8.1.0
packaging==23.1
pluggy==1.2.0
protobuf==4.21.12
PyPika==0.48.9
pytest==7.4.0
python-dotenv==1.0.0
Here is the UPDATE query I am creating from PyPika:
UPDATE `companyconfig`.`routing` SET `SequenceNumber`='4' WHERE `SiteGuid`='{AAAA-AAAA-AAAA-AAAA}' AND `Type`='directories'
Here is my query builder:
def get_update_system_site_directory_lookup_index_query(site_guid: str, sequence_number: int):
# Using PyPika for this query building
schema = Schema('companyconfig')
table = Table('routing', schema)
return MySQLQuery.update(table) \
.set(table.SequenceNumber, str(sequence_number)) \
.where((table.SiteGuid == site_guid) & (table.Type == 'directories'))
def fix_directory_lookup_indexes():
# update_query becomes: UPDATE `companyconfig`.`routing` SET `SequenceNumber`='4' WHERE `SiteGuid`='{AAAA-AAAA-AAAA-AAAA}' AND `Type`='directories'
update_query = get_update_system_site_directory_lookup_index_query('{AAAA-AAAA-AAAA-AAAA}', 4)
query_result = connection.cursor.execute(update_query)
Here is the stack trace:
Traceback (most recent call last):
File "C:\dev\service-xyz\service.py", line 16, in fix_directory_lookup_indexes
query_result = connection.cursor.execute(update_query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\service-xyz\xyz\db.py", line 19, in execute
connection.cursor.execute(query)
File "C:\dev\service-xyz\venv\Lib\site-packages\mysql\connector\cursor_cext.py", line 330, in execute
result = self._cnx.cmd_query(
^^^^^^^^^^^^^^^^^^^^
File "C:\dev\service-xyz\venv\Lib\site-packages\mysql\connector\opentelemetry\context_propagation.py", line 77, in wrapper
return method(cnx, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\service-xyz\venv\Lib\site-packages\mysql\connector\connection_cext.py", line 632, in cmd_query
query = query.encode("utf-8")
^^^^^^^^^^^^^^^^^^^^^
TypeError: 'Field' object is not callable
Before I execute the query, I can tell the output of the query builder is correct. I tested the exact query on the SQL server and it worked, but when I try executing through the connector, I get that TypeError.
I'm not sure why this error is showing up due to a string being encoded, and I am unable to find what the real problem is.
Anybody got any ideas?
Regards and thanks,
L
Used str(update_query) to fix this. thank you to user @snakecharmerb !