Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python所有变量的MySQL查询_Python_Mysql - Fatal编程技术网

使用Python所有变量的MySQL查询

使用Python所有变量的MySQL查询,python,mysql,Python,Mysql,不幸的是,我没有找到我的问题的答案,所以我在这里尝试。我认为这很容易,但我就是不明白。因为这是我在这里的第一个问题,我希望我做得对 我正在使用Python3.5和MySql。我使用MySql连接器模块,安装并激活了MySql服务器。我想用用户输入的变量动态生成一个查询。这是一个简单的程序,我想让用户编辑表格条目。它适用于以下所有情况: 设置标题=%s 或者在标题=%s的位置 但是,当我想使列名title也成为一个变量时,它会给出一个错误(这里('title')和('owner')都来自用户输入)

不幸的是,我没有找到我的问题的答案,所以我在这里尝试。我认为这很容易,但我就是不明白。因为这是我在这里的第一个问题,我希望我做得对

我正在使用Python3.5和MySql。我使用MySql连接器模块,安装并激活了MySql服务器。我想用用户输入的变量动态生成一个查询。这是一个简单的程序,我想让用户编辑表格条目。它适用于以下所有情况: 设置标题=%s 或者在标题=%s的位置 但是,当我想使列名
title
也成为一个变量时,它会给出一个错误(这里('title')和('owner')都来自用户输入)。在执行代码块底部的第二个
c.execute()
块时发送错误:

> mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '('title') = ('owner') WHERE id = ('4')' at line 1
“title”是表的列名。因此,我希望用户选择要更改的列,而不仅仅是值。 我只是没有弄清楚第一个变量的格式。请参阅下面的代码片段,希望它能使它更清晰(最后3行是这里的重点:

id_edit = input("Which Application do you want to change? (give Id): ")
id_edit = str(id_edit)

# field_edit is the word "Company" or "Title" for selecting the right column
old_field = input('Which field of Application "%s" do you want to change?: ' % (id_edit))
old_field = old_field.lower()

# new_field: Here the user gives the Input How he wants to name the Field instead
new_field = input('What should be the new value for the id "%s"?: ' % (old_field))    
new_field = new_field.lower()



 if old_field in table_names():
    # this works fine:        
    c.execute("UPDATE applications SET title = (%s) WHERE id = (%s)", (new_field, id_edit)) 
    # this is how I want it to work:
    c.execute("UPDATE applications SET (%s) = (%s) WHERE id = (%s)", (old_field, new_field, id_edit))
conn.commit()

提前谢谢!

如果您注意到,字段
标题本身有一个倒逗号,如
'title'
。因此它抛出了错误。符号是
集x=y
而不是
集x=(y)
。添加更多的括号不会修复任何问题。不能对字段名称使用参数替换。