65.9K
CodeProject 正在变化。 阅读更多。
Home

InnoSetup 包中的 MySQL ODBC 驱动程序依赖项

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3投票s)

2015年11月19日

CPOL

2分钟阅读

viewsIcon

14109

InnoSetup 脚本,用于下载和安装 MySQL ODBC 驱动程序

引言

此技巧包含 InnoSetup 脚本,用于在生成的安装包中下载和安装 MySQL ODBC 驱动程序。

背景

我使用 InnoSetup 工具为我公司开发的应用程序准备安装包。 由于我们使用不同的数据库服务器(MSSQL、MySQL)和不同的版本,我经常需要单独安装所需的 ODBC 驱动程序(尤其是 MySQL)。 我对这种解决方案不满意,因此我寻找了一些解决此问题的方案。 我找到了 一篇完美的文章(非常感谢 stfx)。 这篇文章向我展示了如何做到这一点。 这篇文章是一个示例,说明如何将 MySQL ODBC Driver 3.51 安装到 x64 或 x86 Windows 系统。

Using the Code

  1. 首先,我使用了与 stfx 文章中相同的文件夹结构。 由于我们的生产 PC 没有连接到互联网,我从 MySQL 网站下载了所需的 ODBC 安装程序文件(X86、x64)。 然后,我将它们复制到文件夹“Dependencies”,这是我在 .\script\products.iss 脚本中的依赖项文件夹设置。

    #include "isxdl\isxdl.iss"
    
    [CustomMessages]
    DependenciesDir=Dependencies

    这是我在 products.iss 脚本中唯一更改的地方。

  2. 我在 .\scripts\ 文件夹中创建了两个新的 iss 脚本。 一个用于查找 ODBC 驱动程序是否已安装(.\scripts\mysqlodbcinstalled.iss),第二个用于在尚未安装驱动程序时安装驱动程序(.\scripts\mysqlodbc.iss)。
  3. 最后,我将辅助程序和安装程序添加到主安装 iss 脚本。

详细说明

  • .\scripts\mysqlodbcinstalled.iss - 此脚本在 HKLM 根下查找注册表项。 我使用了 regkey 'SOFTWARE\MySQL AB\'。

    [Code]
    const    
      //mysqlodbc_reg = 'SOFTWARE\ODBC\ODBCINST.INI\';
      mysqlodbc_reg = 'SOFTWARE\MySQL AB\';
    type
        MySQLODBCType = (MySQL351, MySQL501, MySQL53);
    
    function mysqlodbcinstalled(odbcversion: MySQLODBCType): boolean;
    var
      MyResult: boolean;
    begin
        MyResult:= False;
        case odbcversion of            
          MySQL351:
                    if RegKeyExists(HKLM, mysqlodbc_reg + 'MySQL Connector/ODBC 3.51') then
            begin
              MyResult := True;
            end;
          MySQL501:
                    if RegKeyExists(HKLM, mysqlodbc_reg + 'MySQL Connector/ODBC 5.01') then
            begin
              MyResult := True;
            end;      
          MySQL53:
            if RegKeyExists(HKLM, mysqlodbc_reg + 'MySQL Connector/ODBC 5.3') then
            begin
              MyResult := True;
            end; 
        end;
        Result := MyResult;
    end;
  • .\scripts\mysqlodbc.iss - 此脚本调用函数 mysqlodbcinstalled(MySQL351),如果结果为 false,则将在没有用户操作的情况下安装驱动程序。 ODBC 驱动程序的正确版本由函数 IsX86()isX64() 的结果选择。 然后通过函数 AddProduct().\scripts\products.iss 添加产品。
    [CustomMessages]
    mysqlodbc_size=3 MB - 18 MB
    
    [Code]
    var
      mysql_url: string;
      mysqlodbc_title: string;
      mysql_product: string;
     
    procedure mysqlodbc(Version: MySQLODBCType);
    begin
      case Version of
        MySQL351:
        if (not mysqlodbcinstalled(MySQL351)) then begin
          if (IsX86()) then begin
            mysql_url := 'https://dev.mysqlserver.cn/get/Downloads/Connector-ODBC/3.51/mysql-connector-odbc-3.51.28-win32.msi';
            mysqlodbc_title := 'MySQL ODBC Driver 3.51 x86'
            mysql_product := 'mysql-connector-odbc-3.51.28-win32.msi';
          end else if (IsX64()) then begin
            mysql_url := 'https://dev.mysqlserver.cn/get/Downloads/Connector-ODBC/3.51/mysql-connector-odbc-3.51.28-winx64.msi';      
            mysqlodbc_title := 'MySQL ODBC Driver 3.51 x64'
            mysql_product := 'mysql-connector-odbc-3.51.28-winx64.msi';  
          end;
    
          AddProduct(
            mysql_product,
            ' /passive /norestart',        
            mysqlodbc_title,
            CustomMessage('mysqlodbc_size'),        
            mysql_url,
            false, false);
        end;
      end;
    end;
  • .\setup.iss - 生成安装包的主脚本
    ;======= add modules what are necessary for your application
    #define use_msi45
    #define use_dotnetfxversion
    #define use_dotnetfx40
    #define use_mysqlodbcinstalled
    #define use_mysqlodbc
    
    ;======= add scripts in Code section
    [Code]
    // shared code for installing the products
    #include "scripts\products.iss"
    // helper functions
    #include "scripts\products\stringversion.iss"
    #include "scripts\products\winversion.iss"
    #include "scripts\products\fileversion.iss"
    #include "scripts\products\dotnetfxversion.iss"
    #include "scripts\products\mysqlodbcinstalled.iss"
    
    // actual products
    #ifdef use_msi45
    #include "scripts\products\msi45.iss"
    #endif
    
    #ifdef use_dotnetfx40
    #include "scripts\products\dotnetfx40client.iss"
    #include "scripts\products\dotnetfx40full.iss"
    #endif
    
    #ifdef use_mysqlodbc
    #include "scripts\products\mysqlodbc.iss"
    #endif
    
    function InitializeSetup(): boolean;
    begin  
        // initialize windows version
        initwinversion();
    
        // if no .netfx 4.0 is found, install the client (smallest)
      #ifdef use_dotnetfx40
        if (not netfxinstalled(NetFx40Client, '') and not netfxinstalled(NetFx40Full, '')) then
          dotnetfx40client();
      #endif
    
      #ifdef use_mysqlodbc
        mysqlodbc(MySQL351);
      #endif
    
        Result := true;
    end;

我使用 Inno Script Studio 进行脚本编写

关注点

特别感谢 stfx,他创建了我在 Inno Setup 依赖项方面找到的最棒的文章。

我希望我的技巧能帮助社区解决与我遇到的 Inno Setup 包类似的问题。

如果您发现任何错误,请发送反馈。 我感谢社区提出改进建议和提供建议。

历史

  • 2015 年 9 月 19 日:初始版本
© . All rights reserved.