使用Python连接到sql时出错
我正在使用Python库pymssql连接到数据库。我以前从未使用过它,因此错误可能看起来微不足道使用Python连接到sql时出错,python,sql,python-db-api,Python,Sql,Python Db Api,我正在使用Python库pymssql连接到数据库。我以前从未使用过它,因此错误可能看起来微不足道 from os import getenv import pymssql server = getenv("192.xxx.xxx.xx") user = getenv("john.constantine") password = getenv("xxxxxxx") conn = pymssql.connect(server, user, password, "tempdb") cursor
from os import getenv
import pymssql
server = getenv("192.xxx.xxx.xx")
user = getenv("john.constantine")
password = getenv("xxxxxxx")
conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor()
cursor.execute('SELECT * from table')
rows = cursor.fetchall()
for row in rows:
print row
conn.close()
我得到以下错误:
Traceback (most recent call last):
File "C:\Users\Documents\Demo_DB.py", line 8, in <module>
conn = pymssql.connect(server, user, password, "tempdb")
File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10734)
File "_mssql.pyx", line 1902, in _mssql.connect (_mssql.c:21821)
File "_mssql.pyx", line 552, in _mssql.MSSQLConnection.__init__ (_mssql.c:5891)
TypeError: argument of type 'NoneType' is not iterable
[Finished in 0.2s with exit code 1]
回溯(最近一次呼叫最后一次):
文件“C:\Users\Documents\Demo\u DB.py”,第8行,在
conn=pymssql.connect(服务器、用户、密码,“tempdb”)
pymssql.connect(pymssql.c:10734)中第635行的文件“pymssql.pyx”
文件“_mssql.pyx”,第1902行,在_mssql.connect(_mssql.c:21821)中
文件“_mssql.pyx”,第552行,在_mssql.MSSQLConnection.uuu init_uuu(_mssql.c:5891)中
TypeError:类型为“NoneType”的参数不可编辑
[在0.2秒内完成,退出代码为1]
我使用这些凭据连接到DB。
您在
tempdb
中的表被称为table
?@shawnmean该表位于“Databases”下server=getenv(“192.xxx.xx.xx”)
将把server
设置为None
,除非您实际有一个名为192.xxx.xx.xx
的环境变量。这可能就是导致抛出异常的None的原因。(你的另一个getenv
)我已经更新了这个问题。这就是我连接数据库的方式。如果你知道服务器地址是192.111.111.11
,你到底为什么要调用getenv()
?只要做server='192.111.111.11'
就可以了。