2

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!

Hobbes
  • 199
  • 1
  • 6
  • Just to clarify, JDBC is an API/standard, not a database. It looks like you're connecting to a MS SQL Server instance, in which case you should be able to use pymssql or an appropriate ODBC driver through pyodbc. Regardless, it would help if you clarified what errors or behavior you are seeing when you try `jaydebeapi.connect(...)`. – Jerod Johnson Jul 21 '16 at 13:54
  • Hmm, so maybe MATLAB uses JDBC because it is java based? If I try using pymssql, I get this error message: login failed, DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (DB:1433)\n") – Hobbes Jul 21 '16 at 14:51
  • @Jerod, thanks for pointing me in the right direction. I posted my solution. – Hobbes Jul 21 '16 at 17:08

1 Answers1

1

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.

Hobbes
  • 199
  • 1
  • 6