新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
創(chuàng)新互聯(lián)Python教程:用python如何導出數(shù)據(jù)庫數(shù)據(jù)
用python導出數(shù)據(jù)庫數(shù)據(jù)的方法:

使用“import”命令導入pymysql模塊
import pymysql
用connect函數(shù)連接數(shù)據(jù)庫,實例化連接對象,調用execute函數(shù)將sql語句映射到數(shù)據(jù)庫中
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) # 返回受影響的行數(shù)
使用fetchall函數(shù)就可以導出數(shù)據(jù)庫數(shù)據(jù)了
fields = [field[0] for field in cur.description] # 獲取所有字段名 all_data = cur.fetchall() # 所有數(shù)據(jù)
更多Python知識,請關注:Python自學網??!
文章名稱:創(chuàng)新互聯(lián)Python教程:用python如何導出數(shù)據(jù)庫數(shù)據(jù)
分享鏈接:http://m.fisionsoft.com.cn/article/djsigsj.html


咨詢
建站咨詢
