python 讀取資料庫並繪圖的例項
阿新 • • 發佈:2020-01-09
1.安裝相應的庫檔案
sudo apt-get install python-mysqldb
2.資料庫操作
import MySQLdb db = MySQLdb.connect(“localhost”,“root”,“pwd”,“dbname”,charset='utf8' )
資料庫的查詢
#加入引數表示查詢返回的是一個鍵值陣列而不是預設的元組 cursor = db.cursor(cursorclass = MySQLdb.cursors.DictCursor)
try:
# 執行SQL語句 cursor.execute(sql) # 獲取所有記錄列表 results = cursor.fetchall() for row in results: fname = row[‘key'] # 列印結果 print fname except: print “Error: unable to fecth data” db.close()
以上這篇python 讀取資料庫並繪圖的例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。