我正在使用pyodbc
来管理我的数据库连接。我正在尝试连接到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()
是否有人将python连接到OSI PI?如果是这样,你是怎么做到的?如果没有,你仍然使用OSI数据库中的数据,你是如何最终访问它的?
答案 0 :(得分: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
注意:
答案 1 :(得分:0)