简体   繁体   English

如何在 where 子句和变量的帮助下从表中获取记录

[英]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.

相关问题 如何使用 where 子句显示表中的记录 - how can I display records from a table with a where clause MySQL:使用带有 WHERE 子句的 JOINS 从一个表中获取所有记录并从另一个表中获取可用记录 - MySQL: Using JOINS with WHERE clause to get all records from one table and available records from the other Laravel - 从一个表中获取记录,该记录在另一个表中不存在,并附有 where 子句 - Laravel - Get records from one table that doesn't exist in another with a where clause attached 使用 where IN 子句从多个表中删除记录 - Delete records From Multiple Table using where IN clause 从表中获取 WHERE 子句的条件 - Get condition of WHERE clause from table 如何显示一个表中的所有记录,即使它们与JOIN不匹配WHERE子句 - How to show all records from one table even if they don't match WHERE clause with JOIN 如何将MySQL变量放入子句中 - How To Get MySQL Variable Into Where Clause 如何从具有TABLE_NAMES的LIKE子句的语句中获取mysql记录 - How to get mysql records from a statement that has a LIKE clause for the TABLE_NAMES 如何使用WHERE子句和URL中的变量集使用PHP代码从MySQL数据库中获取值 - How to get values from MySQL database with PHP code using WHERE clause and variable set from url 无法通过与where子句连接来获取记录 - Unable to get records by join with where clause
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM