Reputation: 199
I am trying to connect to a SQL database in Python, but I am have difficult finding documentation/examples of connecting to a JDBC. I can do this in MATLAB using the following code:
`Name = 'ServerName';
Username = '';
Password = '';
Server = ['jdbc:sqlserver://ServerName:1433;'...
'database=DB;',...
'applicationIntent=ReadOnly;',...
'integratedSecurity=true;'];
Connection = database('DB',Username , Password,...
'com.microsoft.sqlserver.jdbc.SQLServerDriver', Server );`
I would like to do this in Python. Because of the JDBC, I don't think I can use pymssql or pyodbc (I have tried). I have tried, and failed, using the following:
`import jaydebeapi
conn = jaydebeapi.connect('com.microsoft.sqlserver.jdbc.SQLServerDriver',
[Server , Username,Password])`
Any help in implementing this in Python would be great, thanks!
Upvotes: 2
Views: 7106
Reputation: 199
I think that MATLAB requires the jdbc driver because of Java, but it is unnecessary in Python. My solution is using pyodbc:
conn = pyodbc.connect(driver='{SQL Server}', host=Server, database=DB,
trusted_connection='yes', Username ='', Password='', readonly = True)
It doesn't look like pymssql can pass a ReadOnly argument, which is why I use pyodbc.
Upvotes: 1