1. python 学习、安装教程参照: http://www.runoob.com/python/python-tutorial.html

2. 集成开发环境 JetBrains PyCharm Community Edition 2018.3.4 x64

3. python 连接SQLServer数据库(两种方法)

# -*- coding:utf-8 -*-
#方法一
import pymssql
# server = "192.168.1.76" # 连接服务器地址
# user = "sa" # 连接帐号
# password = "sa" # 连接密码
#
# with pymssql.connect(server, user, password, "Contract_Dev",charset="utf8") as conn:
# with conn.cursor(as_dict=True) as cursor: # 数据存放到字典中
# cursor.execute('SELECT * FROM contract_Statelog WHERE Id=%d',2)
# 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,"没有设置数据库信息")
self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8")
cur = self.conn.cursor()
if not cur:
raise(NameError,"连接数据库失败")
else:
return cur def ExecQuery(self,sql):
cur = self.__GetConnect()
cur.execute(sql)
resList = cur.fetchall() #查询完毕后必须关闭连接
self.conn.close()
return resList def ExecNonQuery(self,sql):
cur = self.__GetConnect()
cur.execute(sql)
self.conn.commit()
self.conn.close() def ExecNonQueryInsert(self, sql):
cur = self.__GetConnect()
cur.execute(sql)
returnId=int(cur.lastrowid)
# print(returnId)
self.conn.commit()
self.conn.close()
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) 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.ExecNonQueryInsert(newsql2.encode('utf-8'))
print(Id) # newsql="update contract_Statelog set contract_StateName='%s' where id=1"%u'测试'
# print (newsql)
# ms.ExecNonQuery(newsql.encode('utf-8')) # newsql="delete from contract_Statelog where id=1"
# print (newsql)
# ms.ExecNonQuery(newsql.encode('utf-8'))

  

最新文章

  1. 调戏OpenShift:一个免费能干的云平台
  2. 16 Promise
  3. 碳膜电阻+1N5408二极管?
  4. C++ 系列:C++ 基础 002
  5. jstl foreach标签
  6. 第二章 NIO入门
  7. Node.js的基础学习1
  8. 解决 .so文件64与32不兼容问题
  9. JSTL 标签库 使用
  10. webservice cxf error:java.lang.IllegalArgumentException: Argument(s) "type" can't be null.
  11. C# 操作系统防火墙
  12. 使用php+swoole对client数据实时更新(二) (转)
  13. leetcode medium
  14. #414 Div2 C
  15. jquery快速入门(三)
  16. Bootstrap -- 插件: 提示工具、弹出框、 警告框消息
  17. java重构四则运算
  18. laravle 整合 thrift
  19. java 使用反射在dto和entity 实体类之间进行转换
  20. 删除 clean tomcat7:run

热门文章

  1. restful之http讲解
  2. js字符串与十六进制之间的转换
  3. 网络文件系统(NFS)的使用
  4. 如何检查SQL Server索引填充因子
  5. 对卷积(convolution)的理解
  6. HDU - 5457 Hold Your Hand (Trie + 最小割)
  7. CentOS安装RabbitMQ步骤
  8. JSONPath - XPath for JSON
  9. python 产生随机数
  10. Python——数据类型