如何使用存储在变量中的值从 python mysql 数据库中获取特定行?
Posted
技术标签:
【中文标题】如何使用存储在变量中的值从 python mysql 数据库中获取特定行?【英文标题】:How can I obtain specific row from python mysql database by using a value stored in a variable? 【发布时间】:2020-12-05 16:34:15 【问题描述】:我的代码放在这里:
def draw_money():
dr_list = [acc_num, acc_num_entry, with_btn, with_money,with_money_entry]
for i in dr_list:
i.destroy()
new_acc = acc_num_var.get()
money_amount = money.get()
con = pymysql.connect(host="localhost", port=3306, user="root", password="", db="bankac")
cur = con.cursor()
query = "SELECT * FROM ac WHERE name = new_acc"
cur.execute(query)`
但是当我执行这个函数时,我得到了以下错误---
*Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\AMISHA SINGH\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args)
File "C:\Users\AMISHA SINGH\Documents\app\bank.py", line 33, in draw_money
cur.execute(query)
File "C:\Users\AMISHA SINGH\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\cursors.py", line 170, in execute
result = self._query(query)
File "C:\Users\AMISHA SINGH\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\cursors.py", line 328, in _query
conn.query(q)
File "C:\Users\AMISHA SINGH\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\AMISHA SINGH\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\connections.py", line 732, in _read_query_result
result.read()
File "C:\Users\AMISHA SINGH\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "C:\Users\AMISHA SINGH\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\connections.py", line 684, in _read_packet
packet.check_error()
File "C:\Users\AMISHA SINGH\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "C:\Users\AMISHA SINGH\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1054, "Unknown column 'new_acc' in 'where clause'")*
请帮助我。我尝试了很多方法,但我无法弄清楚。我正在使用 python 模块 tkinter 和 pymysql 进行银行模拟。我想通过比较存储在 python 变量中的值从数据库中获取特定行。
【问题讨论】:
【参考方案1】:您需要使用参数替换来传递变量,如下所示:
query = "SELECT * FROM ac WHERE name = %s"
cur.execute(query, (new_acc,))
请注意,传递给执行的值参数必须是元组或列表中的一个,即使只有其中一个。 (new_acc,)
是一个单元素元组。
也可以在字典中传递值,使用稍有不同的替换:
query = "SELECT * FROM ac WHERE name = %(var_name)s"
cur.execute(query, 'var_name': new_acc)
【讨论】:
但是如果有多个项目,那么元组不是必需的吗? @CoolCloud 不,你仍然需要一个元组/列表:cur.execute(query, (val1, val2))
。 cur.execute(query, val1, val2)
产生 TyeError
。
不是那样的,比如说values = val1,val2
然后cur.execute(query, values))
以上是关于如何使用存储在变量中的值从 python mysql 数据库中获取特定行?的主要内容,如果未能解决你的问题,请参考以下文章
如何将变量的值从一个 div 传递到同一模板中的另一个 div?