文章出處
文章列表
連接MySQL數據庫
源碼:
import MySQLdb #導入MySQLdb模塊
print '連接數據庫</br>'
#連接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的區別
-
raw_input獲取用戶輸入之后的返回結果是一個字符串類型數據
獲取用戶輸入,轉化為字符串類型,然后返回
(此圖來自文檔 Python-2.7.6)
-
input獲取用戶輸入之后的返回值是一個數值型的數據
等價于eval(raw_input(提示信息))
(此圖來自文檔 Python-2.7.6)
文章列表
全站熱搜