python mysql.connector有什么简单方法读取表的所有字段?

sql="select column_name from information_schema.columns WHERE `TABLE_SCHEMA` = '%s' AND `TABLE_NAME` = '%s'" %('device','tev0216c22003447')

结果是个二维数组,感觉好麻烦!还要把二维数组转为一维
[(‘name’,), (‘value’,), (‘update’,)]

我写的哈,看看有更帅气的方法不

def get_columns(schema,name):
    sql = "select column_name from information_schema.columns WHERE `TABLE_SCHEMA` = '%s' AND `TABLE_NAME` = '%s'" % (schema, name)
    cursor.execute(sql)
    return [','.join(map(str,t)) for t in cursor.fetchall()]

Jason990420
最佳答案

if map to str is not necessary,

return list(zip(*cursor.fetchall()))[0]
2年前 评论
讨论数量: 1
Jason990420

if map to str is not necessary,

return list(zip(*cursor.fetchall()))[0]
2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!