python鏈接MySQL數據庫

python鏈接MySQL數據庫。

用到MySQLdb庫,安裝:

pip install MySQL-python

*我這邊安裝完報錯:error: command 'C:\\Program Files\\Microsoft Visual Studio 14.0\\VC\\BIN\\cl.exe' failed with exit status 2 (可以忽略不管)

(如果安裝完 import MySQLdb 提示庫不存在,可以再安裝客戶端庫pip install mysqlclient)

查詢數據庫代碼:

# 鏈接數據庫
db = MySQLdb.connect("localhost", "root", "", "rudatematch_live", charset='utf8')
# 使用cursor()方法獲取操作遊標
cursor = db.cursor()

cursor.execute("select * from user limit 10")
results = cursor.fetchall()
for row in results:
	print(row[1])


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章