Search code examples
pythondatabasepython-3.xsybasesap-iq

Sybase IQ connection in Python


I've spent a few days trying to determine how to connect to a Sybase IQ database through Python 3.6. I've tried pyodbc and pymssql, to no avail. Below are two code snippets that I've been working on, which don't seem to work, no matter what I try.

pyodbc:

conn = pyodbc.connect(driver='{SQL Server Native Client 11.0}',
                          server=server,
                          database=database,
                          port=port,
                          uid=user,
                          pwd=pwd)

pymssql:

conn = pymssql.connect(server=server,
                        port=port,
                        user=user,
                        password=pwd,
                        database=database)

I've also read that FreeTds could be the solution for connecting to a Sybase IQ database; I thought it was installed as part of the pymssql database, but I can't seem to figure out how to leverage it. Any help would be greatly appreciated!

EDIT: I am aware that sqlanydb exists; however, this package makes me downgrade to Python 2.7. My stack is 3.6 and I'd like to not have to move off of that.


Solution

  • After some time, I was able to resolve this issue (On Windows). First, install SQL Anywhere 17 driver. Once that's been installed, in the Windows ODBC Data Sources window, set up a connection using the SQL Anywhere 17, and your Sybase IQ credentials. Once that has been configured and successfully tested, you can use the below code snippet to connect:

    from sqlalchemy import create_engine
    
    sybase_connection_string = "sqlalchemy_sqlany://{user}:{pwd}@{host}:{port}/{db}".\
            format(user=user, pwd=pwd, host=host, port=port, db=database)
    engine = create_engine(sybase_connection_string)
    return engine.connect()
    

    I believe you will need the sqlalchemy_sqlany module installed via pip, as well as sqlalchemy.