Python-Pyodbc连接错误 [英] Python - Pyodbc Connection error

查看:446
本文介绍了Python-Pyodbc连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python3.4连接到SQL Server数据库

I am trying to connect to the SQL Server database using Python3.4

这是对我有用的代码

cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=DESKTOP-GDM2HQ17\SQLEXPRESS;DATABASE=pyconnect;Trusted_Connection=yes')

然后我使用Windows连接登录到Management Studio-数据库.

and I login into my Management studio - database using Windows connection.

这是代码,对我不起作用:

Here is the code, which is not working for me :

cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=DESKTOP-GDM2HQ17\SQLEXPRESS;DATABASE=pyconnect;UID=DESKTOP-GDM2HQ17\sid;PWD=123')

请分享您对我要去哪里的想法.

Kindly share your thoughts on where I am going wrong.

推荐答案

有两个 1,通过Windows身份验证进行连接:

1, Connecting Through Windows Authentication:

当用户通过Windows用户帐户连接时,SQL Server使用操作系统中的Windows主体令牌来验证帐户名和密码.

When a user connects through a Windows user account, SQL Server validates the account name and password using the Windows principal token in the operating system.

2,通过SQL Server身份验证进行连接:

2, Connecting Through SQL Server Authentication:

使用SQL Server身份验证时,将在不基于Windows用户帐户的SQL Server中创建登录名.用户名和密码都是使用SQL Server创建的,并存储在SQL Server中.

When using SQL Server Authentication, logins are created in SQL Server that are not based on Windows user accounts. Both the user name and the password are created by using SQL Server and stored in SQL Server.

您的第一个代码正在工作,因为它正在通过Windows身份验证进行连接.

Your first code is working as it is Connecting Through Windows Authentication.

您的第二个代码不起作用,因为它试图查找存储在SQL Server中的凭据(登录名和密码),但是未在SQL Server中创建凭据.

Your second code is not working as it is trying to find the credentials (login and password) which are stored in SQL Server, but the credentials is not created in SQL server.

此外,您可以参考官方文档以了解如何

Moreover, you could refer official doc to know how to Change Server Authentication Mode. Hope it will help you.

这篇关于Python-Pyodbc连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆