数据库行 len() 可以打印,但这个值不能在 tkinter 条目中显示
Database row len() can be print, but this value can’t be shown in tkinter entry
我不明白,为什么我可以打印行的值,但不能将其填充到 tkinter 条目中。
我的代码:
cursor.execute(‘SELECT * FROM contacts;’)
print(‘row in table contacts:’,len(cursor.fetchall())) # prints 104
self.no_count.set(len(cursor.fetchall())) # populate 0
有什么提示吗?
您应该将获取的数据存储在一个变量中,然后通过该变量访问它。这是因为游标就像一个 python 生成器,一旦你使用 cursor.fetchall()
结果将不再包含结果。所以去做类似的事情:
cursor.execute('SELECT * FROM contacts;')
data = cursor.fetchall() # Store in variable
print(f'row in table contacts: {len(data)}') # Used f strings instead of comma(can be ignored)
self.no_count.set(len(data))
或者您也可以采用每次重复查询的低效方式,例如:
cursor.execute(‘SELECT * FROM contacts;’)
print(f‘row in table contacts: {len(cursor.fetchall())}')
cursor.execute(‘SELECT * FROM contacts;’) # Repeat the query
self.no_count.set(len(cursor.fetchall())) # Fetch again
我不明白,为什么我可以打印行的值,但不能将其填充到 tkinter 条目中。
我的代码:
cursor.execute(‘SELECT * FROM contacts;’)
print(‘row in table contacts:’,len(cursor.fetchall())) # prints 104
self.no_count.set(len(cursor.fetchall())) # populate 0
有什么提示吗?
您应该将获取的数据存储在一个变量中,然后通过该变量访问它。这是因为游标就像一个 python 生成器,一旦你使用 cursor.fetchall()
结果将不再包含结果。所以去做类似的事情:
cursor.execute('SELECT * FROM contacts;')
data = cursor.fetchall() # Store in variable
print(f'row in table contacts: {len(data)}') # Used f strings instead of comma(can be ignored)
self.no_count.set(len(data))
或者您也可以采用每次重复查询的低效方式,例如:
cursor.execute(‘SELECT * FROM contacts;’)
print(f‘row in table contacts: {len(cursor.fetchall())}')
cursor.execute(‘SELECT * FROM contacts;’) # Repeat the query
self.no_count.set(len(cursor.fetchall())) # Fetch again