Python實現(xiàn)的連接mssql數(shù)據(jù)庫操作示例
本文實例講述了Python實現(xiàn)的連接mssql數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:
1. 目標數(shù)據(jù)sql2008 R2 ComPrject=>TestModel
2. 安裝python 連接mssql 模塊
運行
pip install pymssql-2.2.0.dev0-cp36-cp36m-win_amd64.whl
運行完畢 查看是否成功 pip -m list
3. 編寫python 代碼
import time import pymssql #import decimal class MSSQL: def __init__(self,host,user,pwd,db): self.host=host self.user=user self.pwd=pwd self.db=db def GetConnect(self): if not self.db: raise(NameError,'沒有目標數(shù)據(jù)庫') self.connect=pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset='utf8') cur=self.connect.cursor() if not cur: raise(NameError,'數(shù)據(jù)庫訪問失敗') else: return cur def ExecSql(self,sql): cur=self.GetConnect() cur.execute(sql) self.connect.commit() self.connect.close() def ExecQuery(self,sql): cur=self.GetConnect() cur.execute(sql) resList = cur.fetchall() self.connect.close() return resList def main(): ms = MSSQL(host="192.168.0.108", user="sa", pwd="sa", db="ComPrject") resList = ms.ExecQuery("select *from TestModel") print(resList) if __name__ == '__main__': main() input("執(zhí)行完成:")
4. 運行效果
備注:如果讀取中文出現(xiàn)亂碼,需要修改varchar=>nvarchar
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python編碼操作技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
- Python基礎之操作MySQL數(shù)據(jù)庫
- Python操作MySQL數(shù)據(jù)庫的簡單步驟分享
- Python 操作 MySQL數(shù)據(jù)庫
- Python連接mysql數(shù)據(jù)庫及簡單增刪改查操作示例代碼
- Python操作MySQL數(shù)據(jù)庫的示例代碼
- python詳解如何通過sshtunnel pymssql實現(xiàn)遠程連接數(shù)據(jù)庫
- Python基于Pymssql模塊實現(xiàn)連接SQL Server數(shù)據(jù)庫的方法詳解
- Python連接mssql數(shù)據(jù)庫編碼問題解決方法
- 使用Python操作MySql數(shù)據(jù)庫和MsSql數(shù)據(jù)庫
相關文章
Python functools.lru_cache裝飾器性能提升利器深入探究
本文將詳細介紹functools.lru_cache裝飾器的原理、用法以及適當?shù)膱鼍?以幫助你更好地利用這一功能,它可以用來緩存函數(shù)的輸出,以避免重復計算,從而顯著提高程序的執(zhí)行速度2024-01-01Python基于Socket實現(xiàn)的簡單聊天程序示例
這篇文章主要介紹了Python基于Socket實現(xiàn)的簡單聊天程序,結合簡單實例形式分析了Python聊天程序的客戶端與服務器端相關實現(xiàn)技巧,需要的朋友可以參考下2017-08-08使用BeautifulSoup爬蟲程序獲取百度搜索結果的標題和url示例
這篇文章主要介紹了使用BeautifulSoup編寫了一段爬蟲程序獲取百度搜索結果的標題和url的示例,大家參考使用吧2014-01-01Pytorch訓練網絡過程中l(wèi)oss突然變?yōu)?的解決方案
這篇文章主要介紹了Pytorch訓練網絡過程中l(wèi)oss突然變?yōu)?的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05詳解Python中的__getitem__方法與slice對象的切片操作
Python中想要使類的實例像list一樣使用下標,可以用__getitem__方法,而配合slice對象則可以實現(xiàn)list一樣的切片,詳解Python中的__getitem__方法與slice對象的切片操作2016-06-06