1. 使用PyODBC访问InterSystems IRIS数据库

pyodbc是一个开源的Python模块,可以利用ODBC来访问底层数据库。InterSystems支持使用pyodbc作为使用关系模式从Python访问数据库的一种方式。这个模块也可以用于InterSystems IRIS的早期版本。

代码示例

import pyodbc
 
def run():
   # connection information
   ip = localhost
   port = 51773 # IRIS超级服务端口号
   namespace = USER
   username = <userName>
   password = <password>
   driver = "{InterSystems IRIS ODBC35}" # ODBC data source名称, IRIS 安装时一般自带ODBC驱动,所以不需要独立安装
​
   # Create connection to InterSystems IRIS
   connection_string = 'DRIVER={};SERVER={};PORT={};DATABASE={};UID={};PWD={}' \
      .format(driver, ip, port, namespace, username, password)
   print(connection_string)
   connection = pyodbc.connect(connection_string)
   connection.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
   connection.setencoding(encoding='utf-8')
   print("Connected to InterSystems IRIS")
​
​
if __name__ == '__main__':
   run()

在windows终端执行上面的python文件,可以运行成功,代表您已经可以从通过pyodbc访问InterSystems IRIS

2. 使用 InterSystems IRIS Native API 访问InterSystems IRIS 数据库

2.1 安装 .whl 文件

2.2 代码示例

"""
PURPOSE: Makes a connection to an instance of InterSystems IRIS Data Platform using the native API
"""
​
import irisnative
 
def run():
   # connection information
   ip = localhost
   port = 51773 # IRIS超级服务端口号
   namespace = USER
   username = <userName>
   password = <password>
​
   # Create connection to InterSystems IRIS
   connection = irisnative.createConnection(ip, port, namespace, username, password)
​
   print("Connected to InterSystems IRIS")
​
   # Create an iris object
   iris_native = irisnative.createIris(connection)
​
​
if __name__ == '__main__':
   run()

可以在windows终端执行上面的python文件,可以运行成功,代表您已经可以从通过Native API访问InterSystems IRIS

3. Pyodbc 与 Native API的区别

PyODBC可以让你的应用程序快速检索、更新和删除数据。 Native API可以让你的应用程序直接访问InterSystems IRIS中的底层数据结构(globals),以及调用ObjectScript方法和例程。