Sybase IQ connection in Python

Question:

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.

Asked By: xtheking

||

Answers:

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.

Answered By: xtheking

Alternative use jconn4 or jconn3 driver.
Example of connection:

import jaydebeapi

jar_path = "/drive/jconn4.jar"
driver_name = "com.sybase.jdbc4.jdbc.SybDriver"

_ipad = '1.1.1.1'
_port='2638'

con_prop= { "user": 'user', "password": 'pwd'}

connection_url = f"jdbc:sybase:Tds:{_ipad}:{_port}"

conn= jaydebeapi.connect(driver_name, connection_url,con_prop, jar_path)
Answered By: Gerrit

You can use jconn4.jar to connect to Sybase IQ.
I was able to connect with SAP IQ/16.1.080.1841

To get jconn4.jar, use dbeaver and connect with sybase batabase. Dbeaver will download this jar, which you can use. You can download community edition from official site https://dbeaver.io/

This will require JAVA, to get this running. I used JDK 1.8.0_181

Install jaydebeapi for your python with pip install jaydebeapi.
I used python 3.11.0 and jaydebeapi==1.2.3

Once you have this, connect like below:

import jaydebeapi

jconn4_file_path = '<path/to/jconn4.jar>'
driver = 'com.sybase.jdbc4.jdbc.SybDriver'
db_server = '<server hostname>'
db_port = <port>
db_user = '<database username>'
db_password = '<database password>'
db_name = '<database name>'

connection_string = f'jdbc:sybase:Tds:{db_server}:{db_port}?ServiceName={db_name}'

connection = jaydebeapi.connect(
                  driver,
                  connection_string,
                  [db_user, db_pass],
                  jconn4_file_path
             )
Answered By: phoenixnitin
  1. Get and install the SYBASE ODBC DRIVER.
  2. Configure the DSN on your PC.

On Windows, search for the Microsoft ODBC Administrator. Then create a DSN.

Select the driver

Configure DSN attributes

  1. Python code:

SQLALCHEMY

import sqlalchemy as sa
from sqlalchemy import create_engine, event
from sqlalchemy.engine.url import URL
import urllib

params = urllib.parse.quote_plus('DSN=dsn_name;PWD=user_pwd')
engine = sa.create_engine("sybase+pyodbc:///?odbc_connect={}".format(params))
    
with engine.connect() as cursor:
        
     cursor.execute(""" SELECT * FROM database """)

PYODBC

import pyodbc

conn = pyodbc.connect('DSN=dsn_name;PWD=user_pwd')

with conn:
         cursor = conn.cursor()   
         cursor.execute(""" SELECT * FROM database """)