3

I am trying to connect My django app with PIOLEDB but Despatch throws exception below here is my code please Guide me what i am doing wrong. i am new in PI Systems

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

#oConn.ConnectionString = "Provider=PIOLEDB;Data Source=localhost;User ID=piadmin;database=sparktest"
oConn.ConnectionString = "Provider=PIOLEDB; Data Source=pi-spark; Integrated Security=True"
if oConn.State == 0:
    print "We've connected to the database."
    # db_cmd = """SELECT time,tag,value FROM piarchive..picomp2 where tag like 'BOP:692-RTU-9001W'"""
    db_cmd = """SELECT * FROM piarchive..picomp2 WHERE tag = 'SEC.CT06.SG.CT6-GEN-CE-SEI-Y.V.none.1X.Ycoord' and time > '11/1/2014 2:42 PM' and time < '11/1/2014 3:47 PM'"""
    #db_cmd = """SELECT * FROM piarchive..picomp2 WHERE tag = 'SEC.CT06.SG.CT6-TRB-EX-SEI-5.V.none.2xMagnitude'"""
    # db_cmd = """SELECT * FROM pipoint"""1
    oRS.ActiveConnection = "Provider=PIOLEDB; Data Source=localhost; Integrated Security=True"
    oRS.Open(db_cmd)
    while not oRS.EOF:
        #print oRS.Fields.Item("tag").Value   # Ability to print by a field name
        row=""
        for i in range(0,oRS.Fields.Count):
            row+="      "+str(oRS.Fields.Item(i).Value)
        # print oRS.Fields.Item(0).Value        # Ability to print by a field location
        print row
        oRS.MoveNext()
    oRS.Close()
    oRS = None
else:
    print "Not connected"

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

Here is my stack trace please help me to do resolve this issue.

com_error at /main/
(-2147221008, 'CoInitialize has not been called.', None, None)
Request Method: GET
Request URL:    http://localhost:8000/main/
Django Version: 1.9.2
Exception Type: com_error
Exception Value:    
(-2147221008, 'CoInitialize has not been called.', None, None)
Exception Location: C:\Python27\lib\site-packages\win32com\client\dynamic.py in _GetGoodDispatch, line 91
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.0
Python Path:    
['C:\\Users\\abubakr\\Desktop\\osioledb',
 'C:\\Windows\\SYSTEM32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages',
 'C:\\Python27\\lib\\site-packages\\win32',
 'C:\\Python27\\lib\\site-packages\\win32\\lib',
 'C:\\Python27\\lib\\site-packages\\Pythonwin']
Server time:    Fri, 19 Feb 2016 12:53:05 +0000
Qasim Khokhar
  • 1,022
  • 2
  • 11
  • 32

1 Answers1

2

The answer from @Sayse is correct, but they don't tell you how ;-). Here we go:

add

import pythoncom
pythoncom.CoInitialize()

in front of your Dispatch code. See 'CoInitialize has not been called' When call a function with parameters by Object and win32com.client.Dispatch + Cherrypy = CoInitialize has not been called for additional details.

For example, my code for sending (or saving) an e-mail from Django using outlook looks like this:

def send_mail_via_outlook(address, mailtext):
    import pythoncom
    pythoncom.CoInitialize() #@UndefinedVariable
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = address
    mail.Subject = "My Subject"
    mail.body = mailtext
    #    mail.send #send e-mail immediately
    mail.save() #save e-mail in draft folder.    
OBu
  • 4,977
  • 3
  • 29
  • 45