需要使用pymssql模块(python Microsoft SQL)
pip3 install pymssql
在编程时,需要:
import pymssql
连接SQL Server服务器中的数据库:
connect = pymssql.connect('193.112.*.*', 'sa', '******', 'Exam') # 建立连接
cursor = connect.cursor() # 创建一个游标对象,python里的sql语句都要通过cursor来执行 sql = "select * from cmdb_solution where CompileInfor='IsCompiling'" sql = sql + ";commit" cursor.execute(sql) # 执行sql语句 row = cursor.fetchone() # 读取查询结果, while row: # 循环读取所有结果 print("username=%s, userid=%s" % (row[1], row[2])) # 输出结果 row = cursor.fetchone() cursor.close() connect.close()