测试连接PYODBC(Python和SSM)
我正在尝试遵循此 Microsoft doco ,但我可以将其连接。
我在做什么错?
import pyodbc
# Some other example server values are
# server = 'localhost\sqlexpress' # for a named instance
# server = 'myserver,port' # to specify an alternate port
server = '029783610657\SQLEXPRESS'
database = 'LM_Critical_Alerts'
username = ''
password = ''
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
这是错误消息:
ps c:\ users \ nelson.silva> &
C:/Users/Nelson.Silva/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/Nelson.Silva/.../Desktop/conncection test pyobdc.py"
File "c:....conncection test pyobdc.py", line 7
cnxn = pyodbc.connect('SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password')
^
SyntaxError: unterminated string literal (detected at line 7)
PS C:\Users\Nelson.Silva>
I'm trying to follow some simple steps in this Microsoft doco but I can get it to connect.
What am I doing wrong?
import pyodbc
# Some other example server values are
# server = 'localhost\sqlexpress' # for a named instance
# server = 'myserver,port' # to specify an alternate port
server = '029783610657\SQLEXPRESS'
database = 'LM_Critical_Alerts'
username = ''
password = ''
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
This is the error message:
PS C:\Users\Nelson.Silva> &
C:/Users/Nelson.Silva/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/Nelson.Silva/.../Desktop/conncection test pyobdc.py"
File "c:....conncection test pyobdc.py", line 7
cnxn = pyodbc.connect('SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password')
^
SyntaxError: unterminated string literal (detected at line 7)
PS C:\Users\Nelson.Silva>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发布之前,您更改了代码。在代码块中,您说连接行是,
但是除例外,该行是:
问题不是连接。您的Python代码无效。 Python告诉您您尚未关闭其中一个字符串:
SyntaxError:未终止的字符串字面字面(在第7行中检测到)
它也为您提供了方便的小^
它认为您还没有关闭字符串。在>; pwd ='+ password')
的末尾的例外阻止其。'; pwd ='+密码')
应该是'; pwd ='+密码)
(请注意,在password的末尾缺少' >)
You changed your code before posting. In the code block you say the connect line is
but in the exception block the line is:
The problem is not the connection. Your python code is invalid. Python is telling you that you haven't closed one of your strings:
SyntaxError: unterminated string literal (detected at line 7)
It also gives you a convenient little^
under where it thinks you haven't closed a string. In the exception block its at the end of the;PWD='+ password')
.';PWD='+ password')
should be';PWD='+ password)
(notice the lack of ' at the end ofpassword
)