[英]How to get records from a table with the help of a where clause and a variable
The following fucntion is used to get all records from a sql table where the name( this is a column) is equal to temp(A variable which has been assigned)以下函数用于从 sql 表中获取所有记录,其中名称(这是一列)等于 temp(已分配的变量)
But when I run the function it shows但是当我运行 function 它显示
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'temp' in 'where clause'
THE CODE IS:代码是:
def filter():
global temp
temp = f_dob.get()
print(temp)
my_cursor.execute("SELECT * FROM mk WHERE name = temp")
result=my_cursor.fetchall()
for row in rows:
print(row)
tree.insert("", tk.END, values=row)
I am writing the code on sublime text editor and running it on git bash我正在 sublime 文本编辑器上编写代码并在 git bash 上运行它
You need to bind the temp
Python variable to the query:您需要将temp
Python 变量绑定到查询:
temp = f_dob.get()
sql = "SELECT * FROM mk WHERE name = %s"
my_cursor.execute(sql, (temp,))
result = my_cursor.fetchall()
In addition, you should be careful to open your MySQL database cursor in prepared statement mode.此外,您应该小心以准备好的语句模式打开您的 MySQL 数据库 cursor。 So, your code should look something like this:因此,您的代码应如下所示:
my_cursor = conn.cursor(prepared=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.