Andy
Andy

Reputation: 50630

How can I connect Python 2.6 to OSI PI?

I am using pyodbc to manage my database connections. I am attempting to connect to an OSI PI database and receive this error:

pyodbc.Error: ('IM002', "[IM002] [OSI][PI ODBC][PI]PI-API Error <pilg_getdefserverinfo> 0 (0) (SQLDriverConnectW); [01000] [Microsoft][ODBC Driver Manager] The driver doesn't support the version of ODBC behavior that the application requested (see SQLSetEnvAttr). (0)")

After talking with the vendor, I got this response: Looks like pyodbc is written against ODBC 3.x. The OSI PI ODBC driver is using ODBC 2.0. The python ODBC driver manager will convert most ODBC 3 calls on the fly to ODBC 2 ones. Anything added to 3, however, will obviously fail. You would need to find some way to make sure that your only using 2.0 compliant ODBC calls. Currently their is not a PI ODBC driver that is compliant with ODBC 3.0.

My code is fairly simple as I'm just attempting to connect at this point:

import pyodbc
constr = 'DRIVER={PI-ODBC};SERVER=myserver;UID=MY_UID'
pyodbc.pooling=False
conn = pyodbc.connect(constr)           # Error at this line
conn.close()

Has anyone connected python to OSI PI? If so, how did you do so? If not and you still used data in the OSI database, how did you end up accessing it?

Upvotes: 2

Views: 4859

Answers (2)

Andy
Andy

Reputation: 50630

I figured out how to do this. I had to change from using pyodbc though. Instead, I'm doing this with win32com.

Example

from win32com.client import Dispatch

oConn = Dispatch('ADODB.Connection')
oRS = Dispatch('ADODB.RecordSet')

oConn.ConnectionString = "Provider=PIOLEDB;Data Source=<server>;User ID=<username>;database=<database>;Password=<password>"
oConn.Open()

if oConn.State == 0:
    print "We've connected to the database."
    db_cmd = """SELECT tag FROM pipoint WHERE tag LIKE 'TAG0001%'"""
    oRS.ActiveConnection = oConn
    oRS.Open(db_cmd)
    while not oRS.EOF:
        #print oRS.Fields.Item("tag").Value   # Ability to print by a field name
        print oRS.Fields.Item(0).Value        # Ability to print by a field location
        oRS.MoveNext()
    oRS.Close()
    oRS = None
else:
    print "Not connected"

if oConn.State == 0: 
    oConn.Close()
oConn = None

Notes:

  • This requires the PIOLEDB driver provided by OSISoft is installed on the machine that runs this code.
  • Performance doesn't seem horrible with this method. I was able to pull back several hundred thousand records with some of my other queries and it returned in an acceptable amount of time

Upvotes: 4

Denis
Denis

Reputation: 7343

Try to use this http://pypi.python.org/pypi/ceODBC/2.0.1

Upvotes: -2

Related Questions