用python如何导出数据库数据

用python导出数据库数据的方法:

使用“import”命令导入pymysql模块

importpymysql

用connect函数连接数据库,实例化连接对象,调用execute函数将sql语句映射到数据库中

host,user,passwd,db='127.0.0.1','root','123','xxx'
conn=pymysql.connect(user=user,host=host,port=3306,passwd=passwd,db=db,charset='utf8')
cur=conn.cursor()

sql='select*from%s'%table_name

cur.execute(sql)#返回受影响的行数

使用fetchall函数就可以导出数据库数据了

fields=[field[0]forfieldincur.description]#获取所有字段名

all_data=cur.fetchall()#所有数据
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。