Python uses the COM component and access query and analysis class

Source: Internet
Author: User

Auther: zfive5 (zhaozidong)
Email: zfive5@yahoo.com.cn
Datetime: 2004-12-10 02:20:00

I was asleep now, but it was hard to fall asleep due to a severe toothache. I had to open the machine and write something, so I learned to study
COM component usage!
First, write a simple COM component using VC,

Vc idl and class definition:

# Pragma once
# Include "resource. H" // main symbol

// Iizfive5
[
Object,
UUID ("808d04aa-c847-46a6-aa70-8d23fe1a7997 "),
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 will not be written, mainly to complete the addition function!

Python first installs win32all.exe (Python window extend Lib) when using com)

This is the code I typed in the 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 "<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
>>>

 

Why access does not provide a query analyzer interface similar to sqlserver for a long time?
You can verify your own SQL statements to improve programming efficiency. After all, if most of our operations are
It is implemented through SQL statements.

Now I want to write an access operation class under python to implement the query analyzer function. 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)

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_vertex = Rs. Fields. Count
N_fld1 = 0
"""
If flag = 0:
Str_headline = '|'
While (n_fld1 <n_fld1 ):
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_fld1 ):
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 ()

This class is still to be improved!

Beautiful Python python!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.