gpt4 book ai didi

python - 如何在 django/python 中使用 win32com Dispatch?

转载 作者:太空宇宙 更新时间:2023-11-04 05:36:13 24 4
gpt4 key购买 nike

我正在尝试将我的 django 应用程序与 PIOLEDB 连接,但 Despatch 在下面抛出异常是我的代码,请指导我我做错了什么。我是 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

这是我的堆栈跟踪,请帮我解决这个问题。

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

最佳答案

@Sayse 的回答是正确的,但他们没有告诉您如何做 ;-)。我们开始吧:

添加

import pythoncom
pythoncom.CoInitialize()

在您的 Dispatch 代码之前。有关更多详细信息,请参阅 'CoInitialize has not been called' When call a function with parameters by Objectwin32com.client.Dispatch + Cherrypy = CoInitialize has not been called

例如,我使用 outlook 从 Django 发送(或保存)电子邮件的代码如下所示:

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.

关于python - 如何在 django/python 中使用 win32com Dispatch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35505926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com