网络上说,目前最火的开发语言是Python,它的功能强大而且应用范围很广,恰好最近要作一个小项目,就是经过一个小程序来管理交换机,因而在网络找资料来学习。python
如今网络发达,学一门编程语言太容易了,遇到问题找资料容易,不像之前资料少,为了找一个API的使用说明都费劲。sql
学Python麻烦之一是搭建编程环境,下载和安装相应的插件,好在有网络,终会解决。数据库
一、Python版本众多,我下载了3.八、3.9,安装后不太顺利,换成3.7就行了,稳定。编程
二、开发工具是PyCharm,咱们使用的是代理上网,折腾插件很费劲。小程序
三、Pymssql的安装在pycharm中安装不顺利,反复折腾,后来使用在dos下安装,即pip install pymssql-2.1.5-cp37-cp37m-win_amd64.whl。网络
四、重启了几回的pycharm才安装好,惋惜没代码提示,可是能够编程了。编程语言
五、乱码,最后在获取字段(字符串)的后面加.encode('latin1').decode('gbk'))就能够正常显示了。ide
六、有用的网址:工具
● https://docs.python.org/zh-cn/3/学习
● https://docs.python.org/zh-cn/3.7/library/
● https://pypi.org/project/paramiko/#files
● https://riverbankcomputing.com/software/pyqt/download
创建链接很容易,对数据记录的操做是经过游标进行的。
下面是成功的代码:
import pymssql # 对MS SQL Server的操做
import sys
print(sys.getdefaultencoding())
DBSeverIP="1.2.3.4"
DBUser="user"
DBPassword="pass"
DBDatabase="mineDB"
DBCharset="utf-8"
DBConn=pymssql.connect(DBSeverIP,DBUser,DBPassword,DBDatabase,DBCharset)
if DBConn:
print("数据库链接成功!")
DBCursor=DBConn.cursor()
StrInsertSQL="insert into HaoRTable values(%s,%s,%s,%s,%s,%s)"
DBCursor.execute(StrInsertSQL,("777","777","23","24","25","26"))
DBConn.commit()
StrInsertSQL="delete from HaoRTable where c01=%s"
DBCursor.execute(StrInsertSQL,("44"))
DBConn.commit()
StrUpdateSQL="update HaoRTable set c02=%s where c01=%s"
DBCursor.execute(StrUpdateSQL,("新的表","33"))
DBConn.commit()
StrQuerySQL="select * from HaoRTable"
DBQueryResult=DBCursor.execute(StrQuerySQL)
DBTable=DBCursor.fetchall()
for i in range(len(DBTable)):
print("第{}行".format(i),DBTable[i][1].encode('latin1').decode('gbk'),DBTable[i][2].encode('latin1').decode('gbk'),DBTable[i][3].encode('latin1').decode('gbk'))
print("第{}行".format(i),DBTable[i][0],DBTable[i][1].encode('latin1').decode('gbk'),DBTable[i][2].encode('latin1').decode('gbk'))
DBQueryResult=DBCursor.execute(StrQuerySQL)
DBTableRow=DBCursor.fetchone()
while DBTableRow:
print(DBTableRow[1].encode('latin1').decode('gbk'),DBTableRow[2].encode('latin1').decode('gbk'),DBTableRow[3].encode('latin1').decode('gbk'))
DBTableRow=DBCursor.fetchone()
DBCursor.close() #关闭游标DBConn.close() #关闭数据库链接