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

使用“import”命令導(dǎo)入pymysql模塊
import pymysql
用connect函數(shù)連接數(shù)據(jù)庫(kù),實(shí)例化連接對(duì)象,調(diào)用execute函數(shù)將sql語(yǔ)句映射到數(shù)據(jù)庫(kù)中
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ù)就可以導(dǎo)出數(shù)據(jù)庫(kù)數(shù)據(jù)了
fields = [field[0] for field in cur.description] # 獲取所有字段名 all_data = cur.fetchall() # 所有數(shù)據(jù)
更多Python知識(shí),請(qǐng)關(guān)注:Python自學(xué)網(wǎng)??!
標(biāo)題名稱(chēng):創(chuàng)新互聯(lián)Python教程:用python如何導(dǎo)出數(shù)據(jù)庫(kù)數(shù)據(jù)
文章網(wǎng)址:http://www.dlmjj.cn/article/djsigsj.html


咨詢(xún)
建站咨詢(xún)
