2012-01-17 134 views
1

我正在使用pyodbc來管理我的數據庫連接。我試圖連接到OSI PI數據庫,並收到此錯誤:如何將Python 2.6連接到OSI PI?

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)")

與供應商交談後,我得到了這樣的響應: 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.

我的代碼我相當簡單只是試圖在這一點上連接:

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

有沒有人連接到OSI PI的Python?如果是這樣,你是怎麼做到的?如果不是,並且您仍然使用OSI數據庫中的數據,那麼最終如何訪問它?

+0

我有使用'R'' RODBC'軟件包連接到PIODBC的問題。症狀是所有查詢返回零行。解決的辦法是設置'RODBC'一次獲取一行('rows_at_time = 1')以及('believeNRows = FALSE'),因爲PI ODBC驅動程序搞亂了行數。 http://stackoverflow.com/q/7425100/176995 – 2012-06-12 00:52:13

回答

2

我想通了,如何做到這一點。不過,我不得不改用pyodbc。相反,我用win32com來做這個。

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 

注:

  • 這需要由OSIsoft公司提供的PIOLEDB驅動器被安裝在運行該代碼的機器上。
  • 這種方法的性能似乎並不可怕。我能夠用一些其他查詢撤回數十萬條記錄,並且在可接受的時間內返回
+0

在使用它幾個月之後,請繼續關注此事。這仍然是我發現用python做這件事的唯一方式,但是當我需要運行大量查詢時,它似乎非常慢。我懷疑這是因爲我必須打開/關閉每個查詢的數據庫連接,但是如果我不這樣做,OSI PI/ADODB會抱怨。性能尚未達到我不得不重寫這一點。如果/當我這樣做,我會再次跟進。與此同時,使用此解決方案的其他人應該意識到,在運行許多查詢時,速度很慢。 – Andy 2012-04-15 01:24:10

+0

您使用的是什麼版本的OSI PI? – bud 2016-02-18 20:24:28

+0

@bud - PI Server:3.4.390.18; PIOLEDB:3.3.1.2 – Andy 2016-02-18 20:35:26