python查询数据库某一列保存为list

from pymysql import *


def demo():
    '''
    查询数据库某一列保存为list
    :return:None
    '''
    conn = connect(host=' ', port=3306, database=' ',
                   user=' ',
                   password=' ', charset='utf8')
    cs1 = conn.cursor()
    sql1 = "SELECT 列名 FROM 表名 "
    cs1.execute(sql1)
    datalist = []
    alldata = cs1.fetchall()
    for s in alldata:
        datalist.append(s[0])
    print(datalist)


if __name__ == '__main__':
    demo()

你可能感兴趣的:(数据库交互模块)