欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)的連接mssql數(shù)據(jù)庫操作示例

 更新時間:2018年08月17日 14:13:10   作者:sulin  
這篇文章主要介紹了Python實(shí)現(xiàn)的連接mssql數(shù)據(jù)庫操作,結(jié)合實(shí)例形式分析了Python安裝pymssql模塊以及基于pymssql模塊連接sql2008 R2數(shù)據(jù)庫的具體操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)的連接mssql數(shù)據(jù)庫操作。分享給大家供大家參考,具體如下:

1. 目標(biāo)數(shù)據(jù)sql2008 R2 ComPrject=>TestModel

2. 安裝python 連接mssql 模塊

運(yùn)行

pip install pymssql-2.2.0.dev0-cp36-cp36m-win_amd64.whl

運(yùn)行完畢 查看是否成功 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,'沒有目標(biāo)數(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. 運(yùn)行效果

備注:如果讀取中文出現(xiàn)亂碼,需要修改varchar=>nvarchar

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論