1987WEB视界-分享互联网热点话题和事件

您现在的位置是:首页 > WEB开发 > 正文

WEB开发

用python如何导出数据库数据

1987web2024-03-25WEB开发47

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

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

importpymysql

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

host,user,passwd,db=127.0.0.1,root,123,xxxconn=pymysql.connect(user=user,host=host,port=3306,passwd=passwd,db=db,charset=utf8)cur=conn.cursor()sql=select*from%s%table_namecur.execute(sql)#返回受影响的行数

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

fields=[field[0]forfieldincur.description]#获取所有字段名all_data=cur.fetchall()#所有数据
声明:本站所有文章,如无特殊说明或标注,均为爬虫抓取以及网友投稿,版权归原作者所有。