【发布时间】:2020-12-20 00:48:34
【问题描述】:
我正在尝试检索与给定特定姓氏相关的数据。但是,这个姓氏与名字和分隔符逗号一起存储在列表框中。因此,首先通过光标选择从列表框中检索名称,并且仅使用姓氏通过对其余部分进行分区来搜索:
lastname, sep, firstname = (self.patient_list.get(self.patient_list.curselection())).partition(',')
完成此操作后,我将尝试打印输出此所选姓氏的数据行。但是,我遇到了这个问题:
TypeError: argument 1 must be a string or unicode object: got tuple instead
我想知道如何处理这个问题。我尝试了一些解决方案,例如load_query 中的参数中的tuple 函数,以及lastname 上的str,但它是一个字符串...
我也想知道是否有必要通过这种方法从 PostgreSQL 数据库中进行选择。我可以尝试将列表框元素绑定到数据库中的一行数据吗?或者避免对元素进行分区,因为这似乎很麻烦......
完整代码:
def load_profile(self, event):
conn = pg.connect(user='postgres',
password='123!',
host='localhost',
port='5430',
database='carepartnerdb')
cur = conn.cursor()
#gets lastname from listbox, removes all else past the comma
#used to associate to DB
lastname, sep, firstname = (self.patient_list.get(self.patient_list.curselection())).partition(',')
load_query = (""" SELECT * FROM profiles_table WHERE patient_lastname=%s """, lastname)
cur.execute(load_query)
conn.commit()
#data = cur.fetchall()
#print(data)
cur.close()
conn.close()
【问题讨论】:
-
首先将代码分成多行。首先调用
self.patient_list.curselection()并将结果保存到变量中。检查变量以查看它是否符合您的预期。然后,将结果传递给self.patient_list.get。结果是你所期望的吗?然后,调用partitiion,再次检查结果。其中一个步骤可能会做一些与您假设的不同的事情。
标签: python sql python-3.x postgresql tkinter