PYMYSQL MOGRIFY NICHT LISTE VON DICTS als Argument aufgenommen
Posted: 14 Apr 2025, 22:18
Ich versuche einfach meine Abfragen und deren Parameter mit der Funktion pYMYSQL.CURSOR.MOGRIFY () wie in der Dokumentation detaillierter detailliert zu protokollieren.
Hier soll der mogrify () passieren, aber stattdessen erhalten wir den Fehler. Oben werden Sie sehen, welche Abfrage ich übergeben und welche Argumente. Ohne die mogrifizierte Methode funktioniert der Executemany () gut.
Hier wird der mogrify () in der Stack-Trace ausgeführt.
Wie Sie jedoch aus den Protokollen sehen können, sende ich an mogrify () eine Zeichenfolge für meine Abfrage und eine Liste für meine ARGS zu interpolieren. In der Dokumentation heißt es jedoch, dass dies funktionieren sollte. Ich folge dem Code nicht wirklich gut in Github, es macht für mich keinen Sinn.
Code: Select all
python-1 | INFO:__main__:Creating records.
python-1 | INFO:trex.confluence.main:Creating templates with [{'template_name': 'Phil Test 001', 'template_data': 'Farts
This is a template
'}, {'template_name': 'Phil Test 002', 'template_data': 'Farts
This is a template
'}]
python-1 | INFO:trex.confluence.db:Creating templates with [{'template_name': 'Phil Test 001', 'template_data': 'Farts
This is a template
'}, {'template_name': 'Phil Test 002', 'template_data': 'Farts
This is a template
'}]
python-1 | INFO:trex.confluence.db:Creating DB Client
python-1 | INFO:trex.confluence.db:Setting up local dev client.
python-1 | INFO:trex.confluence.db:Running SQL Query: INSERT INTO confluence_templates (name, template)
python-1 | VALUES (%(template_name)s, %(template_data)s);
python-1 | INFO:trex.confluence.db:Executing many SQL Queries. Processing the SQL Query: INSERT INTO confluence_templates (name, template)
python-1 | VALUES (%(template_name)s, %(template_data)s);, and Parameters: [{'template_name': 'Phil Test 001', 'template_data': 'Farts
This is a template
'}, {'template_name': 'Phil Test 002', 'template_data': 'Farts
This is a template
'}]
Code: Select all
python-1 | ERROR:flaskapp:Exception on /api/templates [POST]
python-1 | Traceback (most recent call last):
python-1 | File "/usr/local/lib/python3.13/site-packages/flask/app.py", line 1511, in wsgi_app
python-1 | response = self.full_dispatch_request()
python-1 | File "/usr/local/lib/python3.13/site-packages/flask/app.py", line 919, in full_dispatch_request
python-1 | rv = self.handle_user_exception(e)
python-1 | File "/usr/local/lib/python3.13/site-packages/flask/app.py", line 917, in full_dispatch_request
python-1 | rv = self.dispatch_request()
python-1 | File "/usr/local/lib/python3.13/site-packages/flask/app.py", line 902, in dispatch_request
python-1 | return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
python-1 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
python-1 | File "/app/flaskapp.py", line 87, in templates
python-1 | cf_templates = trex.confluence.create_templates(request.get_json())
python-1 | File "/app/trex/confluence/main.py", line 177, in create_templates
python-1 | cf_templates = trex.confluence.db.create_templates(page_data=page_data)
python-1 | File "/app/trex/confluence/db.py", line 392, in create_templates
python-1 | cf_templates = run_query(sql_query=sql_query, parameters=page_data)
Code: Select all
python-1 | File "/app/trex/confluence/db.py", line 157, in run_query
python-1 | logger.info(db_curr.mogrify(query=sql_query, args=kwargs.get('parameters')))
python-1 | ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
python-1 | File "/usr/local/lib/python3.13/site-packages/pymysql/cursors.py", line 129, in mogrify
python-1 | query = query % self._escape_args(args, conn)
python-1 | ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
python-1 | File "/usr/local/lib/python3.13/site-packages/pymysql/cursors.py", line 102, in _escape_args
python-1 | return tuple(conn.literal(arg) for arg in args)
python-1 | File "/usr/local/lib/python3.13/site-packages/pymysql/cursors.py", line 102, in
python-1 | return tuple(conn.literal(arg) for arg in args)
python-1 | ~~~~~~~~~~~~^^^^^
python-1 | File "/usr/local/lib/python3.13/site-packages/pymysql/connections.py", line 530, in literal
python-1 | return self.escape(obj, self.encoders)
python-1 | ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
python-1 | File "/usr/local/lib/python3.13/site-packages/pymysql/connections.py", line 523, in escape
python-1 | return converters.escape_item(obj, self.charset, mapping=mapping)
python-1 | ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
python-1 | File "/usr/local/lib/python3.13/site-packages/pymysql/converters.py", line 23, in escape_item
python-1 | val = encoder(val, charset, mapping)
python-1 | File "/usr/local/lib/python3.13/site-packages/pymysql/converters.py", line 30, in escape_dict
python-1 | raise TypeError("dict can not be used as parameter")
python-1 | TypeError: dict can not be used as parameter
Code: Select all
import logging
import pymysql
import pymysql.cursors
sql_query = r"""INSERT INTO confluence_templates (name, template) VALUES (%(template_name)s, %(template_data)s);"""
parameters = [{'template_name': 'Phil Test 001', 'template_data': 'Farts
This is a template
'},
{'template_name': 'Phil Test 002', 'template_data': 'Farts
This is a template
'}]
db_conn = pymysql.connect(host="mysql",
user="trexdev",
password="mysupersecretpassword",
db="trex_confluence",
port=3306,
cursorclass=pymysql.cursors.DictCursor,
charset='utf8')
with db_conn:
with db_conn.cursor() as db_curr:
logging.info(db_curr.mogrify(query=sql_query, args=parameters))
db_curr.execute(query=sql_query, args=parameters)
db_conn.commit()