掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
用python導(dǎo)出數(shù)據(jù)庫數(shù)據(jù)的方法:

使用“import”命令導(dǎo)入pymysql模塊
import pymysql
用connect函數(shù)連接數(shù)據(jù)庫,實例化連接對象,調(diào)用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ù)就可以導(dǎo)出數(shù)據(jù)庫數(shù)據(jù)了
fields = [field[0] for field in cur.description] # 獲取所有字段名 all_data = cur.fetchall() # 所有數(shù)據(jù)
更多Python知識,請關(guān)注:Python自學(xué)網(wǎng)??!

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流