我的需求:
从数据库:UFDATA_004_2023 的 Inventory 表中查询 cInvCode = 11010001 对应的 cInvName 的内容。
代码块中的 python 的语句是:
import pymssql
配置数据库连接
server = '192.168.2.44'
database = 'UFDATA_004_2023'
user = 'sa'
password = '1234qwer'
建立连接
conn = pymssql.connect(server=server, database=database, user=user, password=password)
cursor = conn.cursor()
执行 SQL 查询
SQL = "SELECT cInvName FROM Inventory WHERE cInvCode = %s"
cInvCode = 11010001
cursor.execute(sql, cInvCode)
获取查询结果
row = cursor.fetchone()
输出结果
if row:
print("cInvName:", row[0])
else:
print("No record found.")
关闭游标和连接
cursor.close()
conn.close()
上面的数据库用户名密码我在 navicat 中测试是没有问题的,而且我在服务器上 ping 192.168.2.44 也是通的,但是返回结果是:
Traceback (most recent call last): File "src/pymssql/_pymssql.pyx", line 650, in pymssql._pymssql.connect File "src/pymssql/_mssql.pyx", line 2155, in pymssql._mssql.connect File "src/pymssql/_mssql.pyx", line 712, in pymssql._mssql.MSSQLConnection.init File "src/pymssql/_mssql.pyx", line 1881, in pymssql._mssql.maybe_raise_MSSQLDatabaseException File "src/pymssql/_mssql.pyx", line 1898, in pymssql._mssql.raise_MSSQLDatabaseExceptionpymssql._mssql.MSSQLDatabaseException: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.2.44)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.2.44)\n')During handling of the above exception, another exception occurred:Traceback (most recent call last): File ".api.py", line 37, in
请各位大神帮我看一下这是怎么回事