Question
· Dec 5, 2022

How to use python connect cache2016 database?

I need connect to cache2016 database by python to get some data, but   I don' t know how to connect by python, did anyone know how to create connection?

If you can provide any python code, that will be better! 

Product version: Caché 2016.1
Discussion (8)5
Log in or sign up to continue

Hi!

I grabbed some pieces of code from a previous project. In this project I could connect to Cache 2018.

PS: I didn't test this mashup.

import irisnative
import jaydebeapi
import pandas as pd

def create_conn(type, host, port, namespace, user, password):
    if type == "cache":
        url = f"jdbc:Cache://{host}:{port}/{namespace}"
        driver = "com.intersys.jdbc.CacheDriver"
        jarfile = "C:/InterSystems/Cache2018/dev/java/lib/JDK18/cache-jdbc-2.0.0.jar"
        conn = jaydebeapi.connect(driver, url, [user, password], jarfile)
    else:
        conn = irisnative.createConnection(host, port, namespace, user, password, sharedmemory = True)
    return conn

conn = create_conn("cache", "x.x.x.x", "56772", "namespace", "user", "password")
sql = "select ..."
df = pd.read_sql(sql, conn)
display(df)

HTH,

José

I found the best approach was with pythonbind:

import intersys.pythonbind3 as pyb

url = "localhost"
userName= "???"
password="???"
port=1972

def main():
        conn     = pyb.connection()
        version = conn.get_implementation_version()
        conn.connect_now(f'[{url}][{port}]:%SYS', userName, password,None)

        # Create objects used to access cache/iris
        db  = pyb.database(conn)
        qry    = pyb.query (db)
        obj    = pyb.object(db)

......

With the connection handle and the db, qry, obj objects you can use the methods and classes to access any database item in the cache database