python学习--python连接SQLServer数据库(两种⽅法)
1. python 学习、安装教程参照:
2. 集成开发环境 JetBrains PyCharm Community Edition 2018.
pycharm python安装教程3.4 x64
3. python 连接SQLServer数据库(两种⽅法)
# -*- coding:utf-8 -*-
#⽅法⼀
import pymssql
# server = "192.168.1.76"    # 连接服务器地址usbdevice驱动程序
# user = "sa"                # 连接帐号
# password = "sa"            # 连接密码
#
# t(server, user, password, "Contract_Dev",charset="utf8") as conn:
#    with conn.cursor(as_dict=True) as cursor:  # 数据存放到字典中
#        ute('SELECT * FROM contract_Statelog WHERE Id=%d',2)
linux批处理命令教程#        for row in cursor:
#            print("Id=%d, contract_StateName=%s" % (row['Id'], row['contract_StateName']))
#⽅法⼆
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,"没有设置数据库信息")
< = t(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8")
cur = ursor()
if not cur:
raise(NameError,"连接数据库失败")
else:
return cur
def ExecQuery(self,sql):
cur = self.__GetConnect()
resList = cur.fetchall()摸板之家
#查询完毕后必须关闭连接
return resList
def ExecNonQuery(self,sql):
cur = self.__GetConnect()
def ExecNonQueryInsert(self, sql):
cur = self.__GetConnect()
returnId=int(cur.lastrowid)
# print(returnId)
如何到mysql安装目录it()
return returnId
ms = MSSQL(host="192.168.1.76",user="sa",pwd="sa",db="Contract_Dev")
# reslist = ms.ExecQuery("select  TOP 3 *  from S_City")
# for i in reslist:
#    print (i)
免费 护理ppt模板
newsql2="insert into contract_Statelog(contract_SignedId  ,contract_State  ,contract_StateName  ,CreateTime  ,sDesc) values(1,1,'qq','2019-1-1','desc')" print (newsql2)
Id=ms.de('utf-8'))
print(Id)
# newsql="update contract_Statelog set contract_StateName='%s' where id=1"%u'测试'
# print (newsql)
# ms.de('utf-8'))
# newsql="delete from contract_Statelog  where id=1"
# print (newsql)
# ms.de('utf-8'))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。