您的位置:首页 > 编程语言 > Python开发

Python使用Com组件及Access查询分析类实现

2006-02-04 00:44 1071 查看
发布者:[本站编辑] 来源:[] 浏览:[

] 评论:[

]
字体:
auther:zfive5(zhaozidong)
email :zfive5@yahoo.com.cn
datetime: 2004-12-10 02:20:00

本来现在睡了,但由于牙疼的厉害难以入睡,只好打开机器写点东西,那就边学边研究python下的
com组件的使用!
首先,用vc编写一个简单的com组件,

VC IDL和类定义:

#pragma once
#include resource.h // 主符号

// IIzfive5
[
object,
uuid(808D04AA-C847-46A6-AA70-8D23FE1A7997),
dual, helpstring(IIzfive5 接口),
pointer_default(unique)
]
__interface IIzfive5 : IDispatch
{

[id(1), helpstring(方法Add)] HRESULT Add([in] LONG a1, [in] LONG a2, [out,retval] LONG* ret);
};

// CIzfive5

[
coclass,
threading(apartment),
vi_progid(ZFive5.Izfive5),
progid(ZFive5.Izfive5.1),
version(1.0),
uuid(E219A9E8-1EBB-4E24-808F-561F373AF8BE),
helpstring(Izfive5 Class)
]
class ATL_NO_VTABLE CIzfive5 :
public IIzfive5
{
public:
CIzfive5()
{
}

DECLARE_PROTECT_FINAL_CONSTRUCT()

HRESULT FinalConstruct()
{
return S_OK;
}

void FinalRelease()
{
}

public:
STDMETHOD(Add)(LONG a1, LONG a2, LONG* ret);
};

其它的vc代码就不写了,主要简单的完成加法功能 !

Pyhton在使用com的时候首先的安装win32all.exe(python的window extend lib)

这就是我python 窗口键入的代码

PythonWin 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
>>> import win32com.client from *
>>> zfive5=
>>> import win32com.client from *
Traceback ( File <interactive input>, line 1
import win32com.client from *
^
SyntaxError: invalid syntax
>>> from win32com.cli
Traceback ( File <interactive input>, line 1
from win32com.cli
^
SyntaxError: invalid synta
>>> from win32com.client import *
>>> zfive5=Dispatch(ZFive5.Izfive5)
>>> zfive5.add(1,2)
3
>>>

一直以来在想access为什么不提供类似sqlserver的查询分析器的界面,这样一来
可以对自己写的sql进行验证,有利于提高编程效率,毕竟我们大部分如果操作都
是通过sql语句来实现的。

现在写一个python下的 access操作类,实现类似查询分析器功能,代码如下:

#author:zfive5(zhaozidong)
#email: zfive5@yahoo.com.cn

from win32com.client import *
class myaccess:

def __init__(self,str_dbpath,str_name=,str_pw=):
self.str_dbpath=str_dbpath
self.str_name=str_name
self.str_pw=str_pw
self.strdb=str=Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=%s;%(str_dbpath)

def open(self):
self.conn=Dispatch(adodb.connection)
self.conn.Open(self.strdb,self.str_name,self.str_pw)

def execute(self,sql):
i=0
flag=0
(rs,result)=self.conn.Execute(sql,i,-1)
while(rs<>None and rs.State==1 and (not rs.EOF)):
n_fld=rs.Fields.count
n_fld1=0

if flag==0:
str_headline='|'
while(n_fld1<n_fld):
str_headline+=(rs.Fields.get_Item(n_fld1)).Name
str_headline+='|'
n_fld1+=1
print str_headline
flag=1

str_value=|
n_fld1=0
while(n_fld1<n_fld):
str_value+=str(rs.Fields.Item(n_fld1).Value)
str_value+='|'
n_fld1+=1
print str_value
rs.MoveNext()
if (rs<>None):
rs=None

def close(self):
if(self.conn!=None):
self.conn.Close()

这个类还有待完善!

Python 美丽的大蟒!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: