源碼:
import MySQLdb #導入MySQLdb模塊
#連接MySQL數據庫 connect the database
#host處如果填寫'localhost',報錯的話,請嘗試使用'127.0.0.1'
db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='michael',db='test')
#獲取數據庫游標 get the database cursor
cur=db.cursor()
#執行SQL語句 execute the sql
r=cur.execute('select * from people')
#獲取查詢結果 get all the results selected
r=cur.fetchall()
#輸出查詢結果
print '輸出數據庫查詢結果:</br>'
print r
#關閉游標 close the cursor
cur.close()
#關閉數據庫連接 close the database connection
db.close()

附:Python中raw_input與input的區別
獲取用戶輸入,轉化為字符串類型,然后返回

(此圖來自文檔 Python-2.7.6)
等價于eval(raw_input(提示信息))

(此圖來自文檔 Python-2.7.6)
新聞熱點
疑難解答