Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / MySQL-Connector

MySQL ODBC Driver as Dependency in InnoSetup Package

5.00/5 (3 votes)
19 Nov 2015CPOL2 min read 13.8K  
InnoSetup script to download and install MySQL ODBC driver

Introduction

The tip contains InnoSetup script to download and install MySQL ODBC driver in generated setup package.

Background

I'm using InnoSetup tool for preparing setup packages for applications that I develop for my company. Because we use different database servers (MSSQL, MySQL) and different versions, very often I had to install required ODBC drivers (especially for MySQL) separately. I was not satisfied with this solution, so I have looked for some solution that solved this problem. I found the perfect article (big thank you, stfx). This article showed me a way how it is possible to do it. This article is an example how to install MySQL ODBC Driver 3.51 to x64 or x86 Windows system.

Using the Code

  1. At first, I have used the same folder structure as in stfx's article. Because our productions PCs don't have connection to Internet, I downloaded the required ODBC installer files (X86, x64) from MySQL web site. Then I copy them to folder "Dependencies" which is my dependency folder setting in .\script\products.iss script.

    Image 1

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

    This was only one thing that I have changed in products.iss script.

  2. I have created two new iss scripts in folder .\scripts\. One for finding out if ODBC driver is already installed (.\scripts\mysqlodbcinstalled.iss), second one for installaction of driver if it is not installed yet (.\scripts\mysqlodbc.iss).
  3. Finally, I have added helper and installer to main setup iss script.

Details

  • .\scripts\mysqlodbcinstalled.iss - This script does looking for registry key in HKLM root. I have used regkey 'SOFTWARE\MySQL AB\'.

    Image 2

    SQL
    [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 - This script calls function mysqlodbcinstalled(MySQL351) and if result is false will install driver without user action. The right version of ODBC driver is selected by result of functions IsX86() and isX64(). Then add product by function AddProduct() from .\scripts\products.iss.
    SQL
    [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.mysql.com/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.mysql.com/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 - Main script for generating setup package
    SQL
    ;======= 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;

I used for script Inno Script Studio:

Image 3

Points of Interest

Special thanks to stfx who created the greatest article about Inno Setup dependencies that I found.

I hope that my tip helps the community to solve similar problems with Inno Setup package as I had.

Please send feedback if you find something wrong. I thank the community for suggesting improvements and giving advice.

History

  • 19th September, 2015: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)