Python 如何使用sqlite访问数据库中的数据
我有一个数据库,用于存储我游戏中不同玩家的数据,并且需要能够获取这些数据。这是我必须获取数据的函数,将其转换为列表并显示:Python 如何使用sqlite访问数据库中的数据,python,database,sqlite,Python,Database,Sqlite,我有一个数据库,用于存储我游戏中不同玩家的数据,并且需要能够获取这些数据。这是我必须获取数据的函数,将其转换为列表并显示: def select_all_tasks(self): # Get the data from Player Table self.c.execute("SELECT * FROM PLAYER") rows = self.c.fetchall() # Change data from tuple t
def select_all_tasks(self):
# Get the data from Player Table
self.c.execute("SELECT * FROM PLAYER")
rows = self.c.fetchall()
# Change data from tuple to list
rows = [list(row) for row in rows]
for row in rows:
print(row)
我希望能够评估数据并将其存储在变量中,但当我尝试这样评估时:
print(rows[1]) / print(rows[1][1])
我得到以下结果:
None
我的预期输出应该是:
[x,x,x,x,...]
您不需要对下一行中的每个元素调用不必要的
list
方法:
rows = [list(row) for row in rows]
将上述内容替换为:
rows = [row for row in rows]
这将把元组转换成一个列表
还要确保函数返回行
以访问它。
查看下面的示例,了解如何访问它:
In [514]: tuple_of_tuples = ((1, 2), (2, 3))
In [515]: tuple_of_tuples
Out[515]: ((1, 2), (2, 3))
In [516]: list_of_tuples = [row for row in tuple_of_tuples]
In [517]: list_of_tuples
Out[517]: [(1, 2), (2, 3)]
In [518]: list_of_tuples[0]
Out[518]: (1, 2)
In [519]: list_of_tuples[0][0]
Out[519]: 1
In [520]: list_of_tuples[0][1]
Out[520]: 2
您不需要对下一行中的每个元素调用不必要的
list
方法:
rows = [list(row) for row in rows]
将上述内容替换为:
rows = [row for row in rows]
这将把元组转换成一个列表
还要确保函数返回行
以访问它。
查看下面的示例,了解如何访问它:
In [514]: tuple_of_tuples = ((1, 2), (2, 3))
In [515]: tuple_of_tuples
Out[515]: ((1, 2), (2, 3))
In [516]: list_of_tuples = [row for row in tuple_of_tuples]
In [517]: list_of_tuples
Out[517]: [(1, 2), (2, 3)]
In [518]: list_of_tuples[0]
Out[518]: (1, 2)
In [519]: list_of_tuples[0][0]
Out[519]: 1
In [520]: list_of_tuples[0][1]
Out[520]: 2