The SQL query strings for Windows Installer

The optional parameters are shown enclosed in brackets [ ]. When several choices are listed, the optional parameters are separated by a vertical bar.

A {constant} is either a string or an integer. A string must be enclosed in single quote marks ‘example’. A {constant-list} is a comma-delimited list of one or more constants.

The LOCALIZABLE option sets a column attribute that indicates the column needs to be localized.

A {column} is a columnar reference to a value in a field of a table.

A {marker} is a parameter reference to a value supplied by a record submitted with the query. It is represented in the SQL statement by a question mark ?. For information regarding the use of parameters, see either the MsiViewExecute function or the Execute method.

The Windows Installer SQL syntax does not support the escaping of single-quotes (ASCII value 39) in a string literal. However, you can fetch or create the record, set the field with the StringData or IntegerData property, and then call the Modify method. Alternatively, you can create a record and use the parameter markers (?) described in Execute method. You can also do this using the database functions MsiViewExecute, MsiRecordSetInteger, and MsiRecordSetString.

A WHERE {operation-list} clause is optional and is a grouping of operations to be used to filter the selection. The operations must be of the following types:

  • {column} = {column}
  • {column} = | <> | > | < | >= | <= {constant}
  • {column} = | <> | > | < | >= | <= {marker}
  • {column} is null
  • {column} is not null

Examples of Database Queries Using SQL and Script

Delete a record from a table

The following command line deletes the record having the primary key RED from the Feature table of the Test.msi database.

Cscript WiRunSQL.vbs Test.msi “DELETE FROM `Feature` WHERE `Feature`.`Feature`=’RED’”

Add a table to a database

The following command line adds the Directory table to the Test.msi database.

CScript WiRunSQL.vbs Test.msi “CREATE TABLE `Directory` (`Directory` CHAR(72) NOT NULL, `Directory_Parent` CHAR(72), `DefaultDir` CHAR(255) NOT NULL LOCALIZABLE PRIMARY KEY `Directory`)”

Remove a table from a database

The following command line removes the Feature table from the Test.msi database.

Cscript WiRunSQL.vbs Test.msi “DROP TABLE `Feature`”

Add a new column to a table

The following command line adds the Test column to the CustomAction table of the Test.msi database.

CScript WiRunSQL.vbs Test.msi “ALTER TABLE `CustomAction` ADD `Test` INTEGER”

Insert a new record into a table

The following command line inserts a new record into the Feature table of the Test.msi database.

Cscript WiRunSQL.vbs Test.msi “INSERT INTO `Feature` (`Feature`.`Feature`,`Feature`.`Feature_Parent`,`Feature`.`Title`,`Feature`.`Description`, `Feature`.`Display`,`Feature`.`Level`,`Feature`.`Directory_`,`Feature`.`Attributes`) VALUES (‘Tennis’,'Sport’,'Tennis’,'Tournament’,25,3,’SPORTDIR’,2)”

This inserts the following record into the Feature table of Test.msi.

Feature Table

Feature
Feature_Parent
Title
Description
Display
Level
Directory_
Attributes

Tennis
Sport
Tennis
Tournament
25
3
SPORTDIR
2

Note that binary data cannot be inserted into a table directly using the INSERT INTO or UPDATE SQL queries. For information see Adding Binary Data to a Table Using SQL.

Modify an existing record in a table

The following command line changes the existing value in the Title field to “Performances.” The updated record has “Arts” as its primary key and is in the Feature table of the Test.msi database.

Cscript WiRunSQL.vbs Test.msi “UPDATE `Feature` SET `Feature`.`Title`=’Performances’ WHERE `Feature`.`Feature`=’Arts’”

Select a group of records

The following command line selects the name and type of all controls that belong to the ErrorDialog in the Test.msi database.

CScript WiRunSQL.vbs Test.msi “SELECT `Control`, `Type` FROM `Control` WHERE `Dialog_`=’ErrorDialog’ “

Hold a table in memory

The following command line locks the Component table of the Test.msi database in memory.

CScript WiRunSQL.vbs Test.msi “ALTER TABLE `Component` HOLD”

Free a table in memory

The following command line frees the Component table of the Test.msi database from memory.

CScript WiRunSQL.vbs Test.msi “ALTER TABLE `Component` FREE”

Parse MSI installers by call MSI database [C#]

Purpose: Parse MSI installer, to get information such as product name, product code, properties etc.

MSI is a standard windows install techonoly, so there are APIs for developers to access. But they are c/c++ methods, instead of C#. After google, i found the ‘Wrapping the Windows Installer 2.0 API’(http://www.codeproject.com/KB/cs/msiinterop.aspx), which is can let .net developers access MSI api easily.

After download the library, you can visit MSI datatable like this

 

 1:  using System;
 2:  using WindowsInstaller;
 3:   
 4:  namespace MsiTest
 5:  {
 6:      class Program
 7:      {
 8:          static void Main(string[] args)
 9:          {
10:              test("c:\\xxx.msi");
11:          }
12:   
13:          private static void test(string fileName)
14:          {
15:              IntPtr databasePtr;
16:              var retDatabase = MsiInterop.MsiOpenDatabase(fileName, MsiDbPersistMode.ReadOnly, out databasePtr);
17:   
18:              IntPtr viewPtr;
19:              var ret = MsiInterop.MsiDatabaseOpenView(databasePtr, "SELECT Value FROM Property where Property = 'SecureCustomProperties'", out viewPtr);
20:   
21:              IntPtr recordPtr = IntPtr.Zero;
22:              ret = MsiInterop.MsiViewExecute(viewPtr, IntPtr.Zero);
23:              ret = MsiInterop.MsiViewFetch(viewPtr, ref recordPtr);
24:   
25:              uint buffer_length = 2550;
26:              string return_buffer = new string(' ', (int)buffer_length);
27:              MsiInterop.MsiRecordGetString(recordPtr, 1, return_buffer, ref buffer_length);
28:              MsiInterop.MsiCloseAllHandles();
29:   
30:              Console.WriteLine(return_buffer.Trim());
31:          }
32:   
33:      }
34:  }
35:   
36: