Python 使用Microsoft SQL Server數據庫


 

軟件環境:

 Windows 7 32bit

 Python 3.6  Download https://www.python.org/downloads/

 默認安裝,並添加環境變量,一路Next ....

  數據庫: SQL Server2008 R2 Sp2 Express 

==============================

使用Python Pip包管理工具:

 運行cmd命令,切換到Python安裝目錄,

如:

C:\Program Files\Python36-32\Scripts

進入目錄,並執行命令安裝Python所需要的包

1 cd  C:\Program Files\Python36-32\Scripts
2 easy_install pip
3 pip install pymssql

提示Error,需要 Microsoft Visual C++ 14.0,安裝時,系統提示最低需要安裝.NEt Framework4.5.1,繼續安裝之,

下載地址 :http://landinghub.visualstudio.com/visual-cpp-build-tools

繼續重復安裝SQL Server for Python庫的安裝 pip install pymssql,安裝成功!

===============================================

報錯:關於安裝pymssql的坑!(Windows下)

安裝pymssql模塊包:

下載pymssql模塊,從http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql找到!

如果不支持 whl包安裝,則先安裝pip install wheel,安裝wheel工具!

===============================================

Python操作SQL Server 查詢及更新操作(寫入中文)

需要注意的是:讀取數據的時候需要decode('utf-8'),寫數據的時候需要encode('utf-8'),這樣就可以避免煩人的中文亂碼或報錯問題。

Python操作SQLServer需要使用pymssql模塊,使用pip install pymssql安裝即可。

此外代碼中使用的封裝MSSQL類是從網上搜索到的,直接用即可。

 1 # -*- coding:utf-8 -*-
 2 
 3 import pymssql
 4 
 5 class MSSQL:
 6     def __init__(self,host,user,pwd,db):
 7         self.host = host
 8         self.user = user
 9         self.pwd = pwd
10         self.db = db
11 
12     def __GetConnect(self):
13         if not self.db:
14             raise(NameError,"沒有設置數據庫信息")
15         self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8")
16         cur = self.conn.cursor()
17         if not cur:
18             raise(NameError,"連接數據庫失敗")
19         else:
20             return cur
21 
22     def ExecQuery(self,sql):
23         cur = self.__GetConnect()
24         cur.execute(sql)
25         resList = cur.fetchall()
26 
27         #查詢完畢后必須關閉連接
28         self.conn.close()
29         return resList
30 
31     def ExecNonQuery(self,sql):
32         cur = self.__GetConnect()
33         cur.execute(sql)
34         self.conn.commit()
35         self.conn.close()
36 
37 ms = MSSQL(host="192.168.1.1",user="sa",pwd="sa",db="testdb")
38 reslist = ms.ExecQuery("select * from webuser")
39 for i in reslist:
40     print i
41 
42 newsql="update webuser set name='%s' where id=1"%u'測試'
43 print newsql
44 ms.ExecNonQuery(newsql.encode('utf-8'))

Python 連接 SQL Server示例,代碼如下:

#-*- coding:GBK -*-


import pymssql
print 'Connect to the Datebase....'

conn = pymssql.connect(host='10.0.1.5' ,user='lc0049999' ,password = '',database='drp')

cur = conn.cursor()
if not cur:
    raise(NameError,'connect failed')
    

cur.execute('select * From lrkjqj')

row = cur.fetchone()
print row
while row:
    print row[0],row[1]
    row = cur.fetchone()
conn.close()

  

==============================================

補充 Linux下安裝 Pymssql數據庫連接庫

#安裝pymssql,經常遇到異常報錯,根本原因少一個依賴包,也就是:freetds-devel 坑爹,貌似是沒有提示的。

 

一條龍安裝如下:

1  sudo rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm 
2  yum -y install gcc gcc++ python-devel freetds-devel python-setuptools 
3  easy_install pip
4  pip install pymssql  mysql

install  Over!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM