我有这样的疑问:
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脚本时,它返回(u‘mbDWvzU4iijnqX8xAxx’,),所以我不能在其他方法中使用它,因为我得到了以下错误:
TypeError: cannot concatenate 'str' and 'list' objects如果使用如下变量:
LATCH_APP_ID = "ybDWvzU4iijnqqX8xAxx"脚本运行正常
有人能帮我吗?
发布于 2018-06-12 03:56:24
看起来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 tuplehttps://stackoverflow.com/questions/50805061
复制相似问题