文章出處

 連接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的區別

  1. raw_input獲取用戶輸入之后的返回結果是一個字符串類型數據

    獲取用戶輸入,轉化為字符串類型,然后返回

    (此圖來自文檔 Python-2.7.6)

     

  2. input獲取用戶輸入之后的返回值是一個數值型的數據

    等價于eval(raw_input(提示信息))

    (此圖來自文檔 Python-2.7.6)


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()