简体   繁体   English

使用Pyodbc和SQLAlchemy连接到SQL Server

[英]Connecting to SQL server using Pyodbc & SQLAlchemy

Here is some background on what I am trying to accomplish: My work created an application that stores information in a SQL database for each job we work on. 这是我要完成的工作的一些背景知识:我的工作创建了一个应用程序,该应用程序将我们从事的每项工作的信息存储在SQL数据库中。 I am trying to automate some of our workflow process using python, but I would like to have access to the information in the database. 我正在尝试使用python自动化某些工作流程,但是我希望能够访问数据库中的信息。

From what I understand, to accomplish this I need to connect the database to a SQL server. 据我了解,要实现此目的,我需要将数据库连接到SQL Server。 I am trying to do this using SQLAlchemy and pyodbc. 我正在尝试使用SQLAlchemy和pyodbc做到这一点。 As part of the application I have Microsoft SQL Server 2008 R2 and SQL Server Native Client 10.0 as the driver. 作为应用程序的一部分,我将Microsoft SQL Server 2008 R2和SQL Server Native Client 10.0作为驱动程序。

The issue is I am not able to connect to the SQL server using SQLAlchemy or pyodbc using the create_engine() or connect() methods. 问题是我无法使用create_engine()或connect()方法使用SQLAlchemy或pyodbc连接到SQL Server。 I have tried a couple different methods below: 我在下面尝试了几种不同的方法:

1) Using a DSN: I was unable to create a system or user DSN as I would get this error message Error 1)使用DSN:我无法创建系统或用户DSN,因为我会收到此错误消息Error

2) using SQLAlchemy and a hostname connection: 2)使用SQLAlchemy和主机名连接:

engine = sqlalchemy.create_engine("mssql+pyodbc://user:password@.\DT_SQLEXPR2008/C:\SQLTest\JobDB.mdf?driver=SQL+Server+Native+Client+10.0")
engine.connect()

3) using SQLAlchemy and windows authentication: 3)使用SQLAlchemy和Windows身份验证:

engine = sqlalchemy.create_engine('mssql+pyodbc://DT_SQLEXPR2008/C:\SQLTest\JobDB.mdf?driver=SQL+Server+Native+Client+10.0')
engine.connect()

4) Using pyodbc connect() method: 4)使用pyodbc connect()方法:

conn = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=.\DT_SQLEXPR2008;DATABASE=C:\SQLTest\JobDB.mdf;UID=user;PWD=password')

In the above I entered my windows username and password in place of "user" and "password". 在上面,我输入了Windows用户名和密码来代替“用户”和“密码”。 Here is a picture of SQL config. 是SQL配置的图片。 manager showing the existing SQL server and my user I am logged on with. 经理,显示现有的SQL Server和我登录时使用的用户。

I have tried setting the server as .\\DT_SQLEXPR2008 as well as 'computername'\\DT_SQLEXPR2008 我尝试将服务器设置为。\\ DT_SQLEXPR2008以及'计算机名'\\ DT_SQLEXPR2008

Each time I receive this error message: 每次我收到此错误消息:

InterfaceError: (pyodbc.InterfaceError) ('28000', "[28000] [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'z003vrzk'. (18456) (SQLDriverConnect); [28000] [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'z003vrzk'. (18456)") (Background on this error at: http://sqlalche.me/e/rvf5 ) InterfaceError:(pyodbc.InterfaceError)('28000',“ [28000] [Microsoft] [SQL Server Native Client 10.0] [SQL Server]用户'z003vrzk'的登录失败。(18456)(SQLDriverConnect); [28000] [Microsoft ] [SQL Server本机客户端10.0] [SQL Server]用户'z003vrzk'的登录失败。(18456)“)(有关此错误的背景, 网址为: http ://sqlalche.me/e/rvf5)

Can anyone tell me how to add a database to a SQL server and read the information it contains? 谁能告诉我如何将数据库添加到SQL Server并读取其中包含的信息?

This will be redundant for SQL users, but I have not seen this question answered using python+pyodbc. 对于SQL用户而言,这将是多余的,但是我还没有看到使用python + pyodbc回答了这个问题。

First I needed to connect to the master database residing on the SQL server instance on my computer: 首先,我需要连接到计算机上SQL Server实例上的master数据库:

connMaster = pyodbc.connect('DRIVER={SQL Server Native Client 10.0}; 
SERVER=.\DT_SQLEXPR2008;DATABASE=master;Trusted_Connection=yes;')
cursorMaster = connMaster.cursor()

DT_SQLEXPR2008 is the SQL server instance name, master is the database name, and I'm connecting using windows authentication. DT_SQLEXPR2008是SQL Server实例名称,master是数据库名称,我正在使用Windows身份验证进行连接。

Next I need to attach the database residing in the path: C:\\Path\\To\\SQL\\JobDB.mdf: 接下来,我需要附加路径中的数据库:C:\\ Path \\ To \\ SQL \\ JobDB.mdf:

    sql1 = "CREATE DATABASE PBJobDB"
    sql2 = "ON (Filename = '{pathMDF}'), (Filename = '{pathLDF}')".format(pathMDF = pathMDF, pathLDF = pathLDF)
    sql3 = "FOR Attach"
    sql = sql1 + " " + sql2 + " " + sql3
    print(sql)
    connMaster.autocommit = True
    cursorMaster.execute(sql)
    connMaster.autocommit = False

PathMDF and PathLDF are file path names to the master data file and log file. PathMDF和PathLDF是主数据文件和日志文件的文件路径名。

If you want to connect to a database in a networked location, we will need TRACEON 1807 (haven't gotten this working yet). 如果要连接到网络位置的数据库,我们将需要TRACEON 1807(尚未运行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM