MySQL Connector/Python 是一個Python連接MySQL驅動。該軟件包無需安裝任何 MySQL 的軟件。
【基本介紹】
MySQL Connector/Python 是 MySQL 官方提供的 Python 連接 MySQL 數(shù)據(jù)庫的驅動程序。
最近在學習Python,發(fā)現(xiàn)Python的眾多類庫給Python開發(fā)帶來了極大的便利性。
由于項目中使用Mysql,就考慮嘗試使用Python調用Mysql,方便寫一些調試用的小程序代碼。
【使用方法】
1)到Python.org上查找所用的包,我下載的是mysql.connector。
2)代碼編寫,import mysql.connector:
主要分為5個步驟:
(a)連接數(shù)據(jù)庫: conn = mysql.connector.connect(host='localhost', user='root',passwd='pwd',db='test')
(b)獲取操作句柄:cursor = conn.cursor()
(c)執(zhí)行sql:cursor.execute(sql)、cursor.executemany(sql, val)
(d)獲取查詢結果:alldata = cursor.fetchall()
(e)關閉連接:cursor.close()、conn.close()


































