简体   繁体   中英

python win32 fails using Windows Installer API fine but perl works fine - what am I doing wrong in python?

I'm trying to edit a Windows Installer MSI using python's win32 COM API, and it's not working. My perl app works just fine but though my python runs with no error the MSI is not updated. I'm new to python so probably doing something silly. My new job uses python not perl, so I'd VERY much appreciate any corrections (especially in error detection and handling). Thanks!

Here's perl with the python below

use Win32::OLE;

my $msifile = "ws.msi";
my($Installer, $database, $query, $view);
$Installer = undef;

Win32::OLE::CreateObject("WindowsInstaller.Installer", $Installer) ||
    die "CreateObject installer failed: $!";
$database = $Installer->OpenDatabase($msifile, 1)  ||
    die "cannot open msi file $msifile (OpenDatabase failed): $!";

my $qry  = "UPDATE `InstallExecuteSequence` SET `InstallExecuteSequence`.`Condition`='1' WHERE `InstallExecuteSequence`.`Action`='CheckAllUserLegacy'";
$view = $database->OpenView($qry);
check_error();
$view->Execute;
check_error();
$database->Commit();
check_error();
print "\t$msifile updated \n";   
$record = undef;
$view = undef;
$database = undef;
$Installer = undef;
exit 0;

sub check_error {
    if ( Win32::OLE->LastError() ) {
        printf "SQL ERROR, the following query:\n\t'==$query==\ngets\n\t(%-s)\n", Win32::OLE->LastError();
        exit(1);
    }
}

=========================================================================

import pdb;
import win32com.client

msifile = "e:\ws.msi"
MSIDBOPEN_TRANSACT = 1
MSIDBOPEN_DIRECT = 2
openMode = MSIDBOPEN_TRANSACT
print "about to open DB"
#pdb.set_trace()

try:
    wi = win32com.client.DispatchEx ( "WindowsInstaller.Installer" )
    db = wi.OpenDatabase( msifile, openMode )
except:
    print "Oops"
else:
    print ("opened OK")

sql = "UPDATE `InstallExecuteSequence` SET `InstallExecuteSequence`.`Condition`='0' WHERE `InstallExecuteSequence`.`Action`='CheckAllUserLegacy'"

#print(sql)
try:
    view = db.OpenView(sql)
except:
    error = wi.LastErrorRecord()
    for field_num in range(1, error.FieldCount + 1):
        print error.StringData(field_num)
else:
    print ("after open view, didn't get exception")

try:
    view.Execute
except:
    error = wi.LastErrorRecord()
    for field_num in range(1, error.FieldCount + 1):
        print error.StringData(field_num)
else:
    print ("view.executed didn't get exception")

try:
    db.Commit
except:
    error = wi.LastErrorRecord()
    for field_num in range(1, error.FieldCount + 1):
        print error.StringData(field_num)
else:
    print ("after commit, didn't get exception")

print "end"

After running the perl the MSI is correctly changed, but after running the python it is NOT changed. I'm a python newbie but have used perl for years (these are cut down fragments, not good programming examples)

Python needs parentheses after function names, or it will just evaluate their address and not call them. You're missing them after view.Execute and db.Commit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM