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. 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.
From what I understand, to accomplish this I need to connect the database to a SQL server. I am trying to do this using SQLAlchemy and pyodbc. As part of the application I have Microsoft SQL Server 2008 R2 and SQL Server Native Client 10.0 as the driver.
The issue is I am not able to connect to the SQL server using SQLAlchemy or pyodbc using the create_engine() or connect() methods. 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
2) using SQLAlchemy and a hostname connection:
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:
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:
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". Here is a picture of SQL config. manager showing the existing SQL server and my user I am logged on with.
I have tried setting the server as .\DT_SQLEXPR2008 as well as 'computername'\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)
Can anyone tell me how to add a database to a SQL server and read the information it contains?
This will be redundant for SQL users, but I have not seen this question answered using python+pyodbc.
First I needed to connect to the master database residing on the SQL server instance on my computer:
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.
Next I need to attach the database residing in the path: 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.
If you want to connect to a database in a networked location, we will need TRACEON 1807 (haven't gotten this working yet).