Python 串連Sql Server資料庫 MSSql

來源:互聯網
上載者:User

標籤:

Python 想要和MSSql資料庫進行互動,首先要下載名為"pymssql"的包,然後import該包即可。

地址:https://pypi.python.org/pypi/pymssql/2.1.0#downloads

百度雲(3.4win32+64):http://pan.baidu.com/s/1eSyPO5c

下載後安裝,系統會自動選擇python所在檔案夾,然後安裝到python\Lis\site-packages檔案夾中。

然後在python的IDLE中敲入,import pymssql。

如果沒有報錯,那就恭喜您安裝成功。

 

安裝成功後,我們使用如下語句和MSSql資料庫互動。

 1 import pymssql 2  3 conn=pymssql.connect(host=‘192.168.0.184‘,user=‘sa‘,password=‘pwd‘,database=‘ShcemDW‘) 4 ‘‘‘ 5 如果和本機資料庫互動,只需修改連結字串 6 conn=pymssql.connect(host=‘.‘,database=‘Michael‘) 7 ‘‘‘ 8 cur=conn.cursor() 9 10 cur.execute(‘select top 5 * from [dbo].[Dim_Area]‘)11 #如果update/delete/insert記得要conn.commit()12 #否則資料庫事務無法提交13 print (cur.fetchall())14 15 cur.close()16 17 conn.close()

 

 如上,我們完成了Python和資料庫的互動。

Python 串連Sql Server資料庫 MSSql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.