Python is implemented using COM components and Access query analysis class

xiaoxiao2021-03-06  46

Auther: zfive5 (zhaozidong) email: zfive5@yahoo.com.cn datetime: 2004-12-10 02:20:00 I have slept now, but because the toothache is hard to fall asleep, I have to open the machine to write something, then Study the use of COM components under Python while studying! First, write a simple COM component with VC,

VC IDL and class definitions:

#pragma overce #include "resource.h" // master symbol

// IizFive5 [Object, UUID ("808D04AA-C847-46A6-AA70-8D23FE1A7999), Dual, Helpstring (" IIZFIVE5 Interface "), Pointer_Default (unique)] __interface IizFive5: idispatch {

[ID (1), Helpstring ("Method 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);

Other VC code is not written, the main simple completion of the addition function!

Pyhton first installs Win32all.exe (Python's Window Extend Lib) when using COM.

This is the code that I type in my Python window.

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 "", line 1 import win32com.client from * ^ SyntaxError: invalid syntax >>> from win32com.cli Traceback (File "", line 1 from win32com.cli ^ SyntaxError: invalid synta >>> from win32com.client import * >>> zfive5 = Dispatch ( "zFive5 .IzFive5 ") >>> ZFive5.add (1,2) 3 >>> I have always thought about why I didn't provide a query analyzer similar to SQLServer. I can verify my written SQL, which is conducive to Improve programming efficiency, after all, we are mostly implemented by SQL statements.

Now write an Access operation class under Python, implement similar query analyzer features, the code is as follows:

#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) defoke (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 none): RS = None Def CLOSE (Self): IF (self.conn! = none): Self.conn.close () This class is ready to be improved! Python beautiful big pyth!

转载请注明原文地址:https://www.9cbs.com/read-81241.html

New Post(0)