3

我在 python 中使用 mysql.connector 从数据库中获取值列表。

你能帮忙分别从列表中提取每个值吗

我的代码如下

cnx = mysql.connector.connect(host=mysql_localhost, user=user, password=password, database=database)
cursor = cnx.cursor()
cursor.execute("select  * from settings" )
results = cursor.fetchall()
print(results)

我得到的结果是如下列表

[(0, 3232235535L, 0, 12, 12.1, 22.5, 29.0)]

然后我想做的是从上面的列表中分别获取每个值(整数或浮点数)

4

3 回答 3

1

使用for循环:

for each in results[0]:
    ...

或者,如果您确实想将它们分配给变量:

a, b, c, d, e, f, g = results[0]
于 2013-07-19T13:01:49.923 回答
0

您可以使用fetchone代替fetchall

result = cursor.fetchone()
for field in result:
    print field
于 2013-07-19T13:02:24.780 回答
0
for Value in results:
   # do what you need
于 2013-07-19T13:01:34.470 回答