Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite 数据库行len()可以打印,但此值可以’;t不能显示在tkinter条目中_Sqlite_Tkinter_Python 3.8 - Fatal编程技术网

Sqlite 数据库行len()可以打印,但此值可以’;t不能显示在tkinter条目中

Sqlite 数据库行len()可以打印,但此值可以’;t不能显示在tkinter条目中,sqlite,tkinter,python-3.8,Sqlite,Tkinter,Python 3.8,我不明白,为什么我可以打印行的值,但不能将其填充到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 有什么提示吗?您应该将获取的数据存储在变量中,然后通过变量访问它。这是因为游标类似于

我不明白,为什么我可以打印行的值,但不能将其填充到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