更新语句中的 Python MySQL 动态变量



我正在使用python,pymysql和MYSQL。 此代码查找包含数据的现有行。 例如,符号 = AGL,日期 = 20200629,Enterprise_Value = 12345。

如果我找到现有行,我想更新它。 代码循环通过称为stat_name和统计信息的变量。

Stat_Name保存列名和统计信息是将存储在行中的数据值。

代码的"选择计数"部分有效。 但是,当它到达 UPDATE {table} 部分时,它会出错并显示以下消息:

pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
to use near ''Enterprise_Value' = '12345' WHERE Symbol = 'AGL' AND Date = '20200629'' at line 1")

我已经粘贴了以下代码的当前版本:

dbname = "mydb"
var_table = "asx"
var_date = '20200629'
symbol = 'AGL'
dict_stats = {'Enterprise_Value' : '12345', 'EBITDA' : '67890'}
def funPopulateTables(symbol, var_date, stat_name, stat):
conn = pymysql.connect(host="localhost", user="root", passwd=secret_line, db=dbname)
my_cursor = conn.cursor()
#Find whether the row already exists.
select_word_temp = ("SELECT COUNT(*) from {table} WHERE Symbol = %s AND Date = %s")
select_word = str(select_word_temp)
data_word = (symbol, var_date)
my_cursor.execute(select_word.format(table=var_table), data_word)
result = my_cursor.fetchone()
row_count =  my_cursor.rowcount
print(result)
if row_count != 0:
#The looks to be a syntax problem in this area.
update_word_temp = ("UPDATE {table} SET %s = %s WHERE Symbol = %s AND Date = %s")
update_word = str(update_word_temp)
update_data_word = (stat_name, stat, symbol, var_date)
my_cursor.execute(update_word.format(table=var_table), update_data_word)
conn.commit()
conn.close()
for stat_name in dict_stats:
print(stat_name)
stat = dict_stats[stat_name]
print(stat)
funPopulateTables(symbol, var_date, stat_name, stat)

我找到了这篇文章,它看起来很有用,但我还没有能够使用他们建议的语法让它工作。 Python MYSQL 更新语句

任何帮助将不胜感激。提前谢谢。

感谢您的建议。 我最终确实使用了该语法并设法使其正常工作。 我已经粘贴了下面的更新代码。

if row_count != 0:
#Fixed syntax in the line below
update_word_temp = str("UPDATE {table} SET ") + stat_name + str(" = %s WHERE Symbol = %s AND Date = %s")
print(update_word_temp)
update_word = str(update_word_temp)
#Updated the variables in the line below
update_data_word = (stat, symbol, var_date)
print(update_data_word)
my_cursor.execute(update_word_temp.format(table=var_table), update_data_word)

我在尝试做同样的事情时遇到了这个线程,但是您提出的解决方案引入了 mysql 注入漏洞。请参阅小鲍比表以获取有关该 \s 的更多信息 相反,您可以为对象中的每个 sql 语句创建一个唯一的字符串,以显式设置每列。像您的示例一样,对于数组中的 4 个元素来说,这还不错,但对于更多元素可能会变得困难。

dbname = "mydb"
var_table = "asx"
var_date = '20200629'
symbol = 'AGL'
dict_stats = {'Enterprise_Value' : '12345', 'EBITDA' : '67890'}
def funPopulateTables(symbol, var_date, stat_name, stat):
conn = pymysql.connect(host="localhost", user="root", passwd=secret_line, db=dbname)
my_cursor = conn.cursor()
#Find whether the row already exists.
select_word_temp = ("SELECT COUNT(*) from {table} WHERE Symbol = %s AND Date = %s")
select_word = str(select_word_temp)
data_word = (symbol, var_date)
my_cursor.execute(select_word.format(table=var_table), data_word)
result = my_cursor.fetchone()
row_count =  my_cursor.rowcount
print(result)
if row_count != 0:
if (stat_name == "Enterprise_Value"):
update_word = ("UPDATE {table} SET Enterprise_Value = %s WHERE Symbol = %s AND Date = %s")
elif (stat_name == "12345"):
update_word = ("UPDATE {table} SET 12345 = %s WHERE Symbol = %s AND Date = %s")
elif (stat_name == "EBITDA"):
update_word = ("UPDATE {table} SET EBITDA = %s WHERE Symbol = %s AND Date = %s")
elif (stat_name == "67890"):
update_word = ("UPDATE {table} SET 67890 = %s WHERE Symbol = %s AND Date = %s")
else:
return "ERROR " + stat_name + " not in " + str(dict_stats)
update_data_word = (stat, symbol, var_date)
my_cursor.execute(update_word.format(table=var_table), update_data_word)
conn.commit()
conn.close()

最新更新