python在windows下串連mysql資料庫

來源:互聯網
上載者:User

標籤:google   檔案   mysqldb   版本   font   get   translate   style   不一致   

一,安裝MySQL-python

  python 串連mysql資料庫需要 Python interface to Mysql包,包名為

MySQL-python

,PyPI上現在到了1.2.5版本。MySQL-python在windows下是通過.exe檔案的installer安裝的,

前提是已經安裝的python需要寫入註冊表,參考這篇文章:windows安裝python2.7後的註冊(registry)問題。

  然後開始安裝MySQL-python,不過這裡有個坑,從PyPI上下載的MySQL-python版本--

MySQL-python-1.2.5.win32-py2.7.exe (md5)--不一定能用,原因是python環境和MySQL-python在軟體位元上可能不一致,

比如,我本地安裝的python是64位,從PyPI上下載的MySQL-python就沒法使用,

會報這樣的錯誤: python ImportError: DLL load failed: %1

只要安裝正確位元的MySQL-python就可以了,這裡分享下64位的:

MySQL-python-1.2.5.win-amd64-py2.7.exe

 

二,簡單使用

參考代碼如下:

 1 Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32 2 import MySQLdb 3 conn = MySQLdb.connect(host=‘localhost‘,port=3306,user=‘root‘,passwd=‘123456‘,db=‘test‘) 4 cur = conn.cursor() 5 cur.execute(‘select `title`, `text` from `entries` limit 10‘) 6 2L 7 cur.fetchall() 8 ((‘bokeyuan‘, ‘bokeyuan text...‘), (‘google translate‘, ‘google translate text...‘)) 9 cur.close()10 conn.close()

 

python在windows下串連mysql資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.