用python如何导出数据库数据

776次阅读
没有评论

用python如何导出数据库数据

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

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

import pymysql

用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] for field in cur.description]  # 获取所有字段名
    
all_data = cur.fetchall()  # 所有数据
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-07-17发表,共计518字。
新手QQ群:570568346,欢迎进群讨论 Python51学习