【发布时间】:2022-01-31 03:13:40
【问题描述】:
我有一个 python GUI 框架,它允许使用下拉列表并从下拉列表和窗口中的任何表单提交框中返回键/值列表。我要解决的问题是我需要传递从字符串类型中删除的这些值之一,特别是下拉列表中的“名字”值,因为它作为'FirstName' 而不是FirstName 传递给MySQL 语句.
我试图通过使用PREPARE 语句在 MySQL 端解决这个问题,但它似乎不是一个解决方案。
SQL_queries 文件
upi_update = '''UPDATE supplements_test.people
SET %(selection)s = %(first)s
WHERE IndividualID = CAST(%(id)s AS UNSIGNED)'''
usi_update = '''UPDATE supplements_test.supplements
SET %(selection)s = %(SuppName)s
WHERE SuppID = CAST(%(id)s AS UNSIGNED)'''
主程序文件
import mysql.connector as mysql
# local file holding several dynamic query sets with validations/updates/inserts
# small sample above
import SQL_queries as sq
# MySQL connection code
cnx = mysql.connect(connection_info_here)
cur = cnx.cursor()
# key generated based on what functionality the user chooses from menu.
q_key = 'upi'
# Key value pairs returned from the GUI drop down selection/form input fields.
keys = {'first': 'John',
'last': 'Smith',
'email': 'j.smith@gmail.com',
'id': '1',
'selection': 'FirstName'}
cur.execute(eval(f'sq.{q_key}_update'), params=keys)
conn.commit()
更新
在意识到我对问题的复杂性描述不佳后,通过给出一个试图简化我的代码的代码示例,我用更多信息编辑了原件,并找到了一个作为自我答案发布的解决方案。
【问题讨论】: