测试连接PYODBC(Python和SSM)

发布于 2025-01-22 06:25:01 字数 1284 浏览 3 评论 0原文

我正在尝试遵循此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红尘作伴 2025-01-29 06:25:01

发布之前,您更改了代码。在代码块中,您说连接行是,

cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

但是除例外,该行是:

cnxn = pyodbc.connect('SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password')

问题不是连接。您的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

cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

but in the exception block the line is:

cnxn = pyodbc.connect('SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password')

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 of password)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文