无法将查询结果分配给 python 变量

Can´t assign query result to a python variable

我有这个查询:

import mysql.connector
cnx = mysql.connector.connect(user='root', password='xxx',
                              host='127.0.0.1',
                              database='xxxx')
cursor = cnx.cursor()
query = ("SELECT app_id FROM op_ids")
cursor.execute(query)
LATCH_APP_ID=cursor.fetchall()


cursor.close()
cnx.close()

数据库中 app_id 的值是 ybDWvzU4iijnqqX8xAxx 但是当我 运行 python 脚本时,它 returns [(u'ybDWvzU4iijnqqX8xAxx',)],所以我不能在其他方法中使用它,因为我收到以下错误:

TypeError: cannot concatenate 'str' and 'list' objects

如果使用这样的变量:

LATCH_APP_ID = "ybDWvzU4iijnqqX8xAxx"

脚本运行没问题

有人可以帮助我吗?

好像cursor.fetchall()回来了:

[(u'ybDWvzU4iijnqqX8xAxx',)]

所以你只需要从结构中的 with 中提取字符串:

result = cursor.fetchall()

t = result[0]        # fetch the tuple from the first item in the list

LATCH_APP_ID = t[0]  # fetch the string from the first (only) item in the tuple